2

If i have an array $output that looks like this, how can i search the array and echo out the duration value which in this case is 30. Duration is not always key [18].

Array
(
    [16] =>     hasKeyframes    : true
    [17] =>     hasMetadata     : true
    [18] =>     duration        : 30
    [19] =>     audiosamplerate : 22000
    [20] =>     audiodatarate   : 68
    [21] =>     datasize        : 1103197
}
3
  • 6
    be so much easier for you, if you re/built your array properly Commented Jun 7, 2011 at 7:29
  • @Lawrence This is the array generated from doing exec('ffmpeg -i video.flv', $output); I did not make it this way Commented Jun 7, 2011 at 7:31
  • 2
    but you could rebuild it by looping it with explode(':',$output[$i]) Commented Jun 7, 2011 at 7:32

5 Answers 5

5

Try this function:

function search_value($array, $key, $default_value = false)
{
   foreach( $array as $value)
   {
      list($_key, $_val) = array_map('trim', explode(":", $value) );
      if( strtolower($key) == strtolower($_key) )
         return $_val;
   }
   return $default_value;
}

use it like this:

echo search_value( $output, 'duration', 0);

BUT, just like @Lawrence pointed out, it will be MUCH easier if you change your array structure:

$output= array(
  'hasKeyframes'=>true,
  'hasMetadata'=>true,
  'duration'=>'30',
  .
  .
  .
);

This way, you only have to check if key exist and get that value:

echo (!array_key_exists('duration', $output) ? $output['duration'] : 0);
Sign up to request clarification or add additional context in comments.

1 Comment

read my comment back to lawrence. this is how exec() outputs the array. In your example search_value($array, $key) What is $key. Do i need to input that in. $key is not known. It's dynamically generated.
2

I recommend to sanitize the array at first:

$sanitized = array();

foreach($output as $value) {
    $data = explode(':', $value);
    $sanitized[trim($data[0])] = trim($data[1]);
}

echo $sanitized['duration'];

Comments

2

Can't you make it associative array instead?

Sure he can:

<?php
$output = array(
    16 => 'hasKeyframes    : true',
    17 => 'hasMetadata     : true',
    18 => 'duration        : 30',
    19 => 'audiosamplerate : 22000'
    20 => 'audiodatarate   : 68',
    21 => 'datasize        : 1103197'
);

foreach( $output as $element ) {
    $values = array_map( 'trim', explode( ':', $element ) );
    $assoc[$values[0]] = $values[1];
}


echo isset( $assoc['duration'] ) ? $assoc['duration'] : 'Duration not set.';

Comments

1
$array=preg_grep("/duration/", $output);
$array=implode(",",$array);
$key_value=explode(":",$array);
echo $key_value[1];

2 Comments

This isn't working. What is $array in your code. My array name is $output.
Thanks, it works. All other answers are valid, but this is short and clean. BTW you made a typo $output)). It should be $output)
0

Can't you make it associative array instead?

Array
(
    [hasKeyframes] =>    true
    [hasMetadata] =>     true
    [duration] =>        30
    [audiosamplerate] => 22000
    [audiodatarate] =>   68
    [datasize] =>        1103197
}

Then it would be simple $arr['duration'].

If not, you can parse values, for example in the way like silent posted meanwhile.

1 Comment

@Pinkie: I agree with Lawrence Cherone who 3 hours ago wrote in comment to question: but you could rebuild it by looping it with explode(':',$output[$i])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.