-1

I know little bit about foreach loop. I am trying to access certain value or pointer each time in this array.

[formats] => Array
    (
        [0] => Array
            (
                [format] => nondash-171 - audio only (DASH audio)
                [vcodec] => none
                [format_note] => DASH audio
                [abr] => 128
                [url] => 
                [ext] => webm
                [preference] => -10050
                [format_id] => nondash-171
            )

        [1] => Array
            (
                [format] => nondash-140 - audio only (DASH audio)
                [url] => none
                [format_note] => DASH audio
                [preference] => -10050
                [format_id] => nondash-140
            )

        [2] => Array
            (
                [url] => http://
                [format] => nondash-278 - 144p (DASH video)
                [preference] => -10040
                [format_id] => 130
            )

The value of array will change every time depending on the query I am not sure in which pointer the value contains. For ex.

$value = $json[formats][0][url]

I am making myself clear more about my intention. I intend to do like this.

if ($json[formats][0][format_id]=="130")
    { $url_130 = $json[formats][0][url]}

But I am not sure in which pointer the value '130' will be hold.It can be anything like

 $json[formats][1][format_id]=="130"  

or

 $json[formats][2][format_id]=="130"

So I need to check every pointer in which the value [format_id]=="130" will be carried. And then setting the correct url with correct pointer.

1 Answers1

0

If you don't need the other information you could create an array with just the url and use the id as array index. I am assuming your format_id s are numbers.

$new_array = array();

foreach ( $formats as $format) {
   $new_array[$format['format_id']] = $format['url']; 
}

after that you can access using the index directly

$url = $new_arry[130];
RST
  • 3,899
  • 2
  • 20
  • 33