简体   繁体   中英

PHP Array catch key of key value pair

Well, not sure I am going to be able to write this out to well, but I will try. From a backend script I can't really change up to much. I have a very large multi-dimensional array being spit out to the UI where the array's within the main array dont contain your normal 0-n index scheme, and they are generated off the backend due to the association they have. So I have for example a piece of the multi-dimensional array that looks like

Array(
     [0] = Array(
               [stuff] = 'something'
               [morestuff] = 'other'
               [info] = array(
                               [generated_id_based_on_assication] = array(
                                                              [id] = '12345t'
                                                              [desc] = 'blah blah'
                                                              [url] = '/some/where'
                                                              )
                               [generated_id_based_on_assication] = array(
                                                              [id] = '12345t'
                                                              [desc] = 'blah blah'
                                                              [url] = '/some/where'
                                                              )
                               [generated_id_based_on_assication] = array(
                                                              [id] = '12345t'
                                                              [desc] = 'blah blah'
                                                              [url] = '/some/where'
                                                              )
                               [generated_id_based_on_assication] = array(
                                                              [id] = '12345t'
                                                              [desc] = 'blah blah'
                                                              [url] = '/some/where'
                                                              )
                        )
              ),
     [1] = Array(
               [stuff] = 'something'
               [morestuff] = 'other'
               [info] = array(
                               [generated_id_based_on_assication] = array(
                                                              [id] = '12345t'
                                                              [desc] = 'blah blah'
                                                              [url] = '/some/where'
                                                              )
                               [generated_id_based_on_assication] = array(
                                                              [id] = '12345t'
                                                              [desc] = 'blah blah'
                                                              [url] = '/some/where'
                                                              )
                               [generated_id_based_on_assication] = array(
                                                              [id] = '12345t'
                                                              [desc] = 'blah blah'
                                                              [url] = '/some/where'
                                                              )
                               [generated_id_based_on_assication] = array(
                                                              [id] = '12345t'
                                                              [desc] = 'blah blah'
                                                              [url] = '/some/where'
                                                              )
                        )
              )
       )

I know not the best representation of an array. But for the sake of example as I can't post the actual data itself what I need to do is for the [info] array find each generated_id_based_on_assication so I can pull data from each generated_id_based_on_assication array. But seeing as its not a 0-n index Im not sure how to grab that "generated_id_based_on_assication" part so I can work with the data within it, as running it through a foreach or any type of loop really isn't an option for that particular array within the arrays. Anyone have a suggestion? If I was able to run this through a loop I could do it, but thats where im tripped up I can't as this data is being listed in tables, and everything is on a per row basis for that array.

Assuming your outer array is in a variable called $array.. Try

$keys = array_keys($array[0]['info']);

and then $keys will contain the generated ids and you can use them like this

$desc = $array[0]['info'][$keys[0]]['desc'];

http://php.net/manual/en/function.array-keys.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM