PHP数组捕获键值对的键

时间:2021-03-23 22:08:25

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

好吧,不确定我是否能够把它写得很好,但我会尝试。从后端脚本我无法真正改变。我有一个非常大的多维数组向UI吐出,主阵列中的数组不包含正常的0-n索引方案,并且由于它们之间的关联而在后端生成它们。所以我有一块看起来像的多维数组

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.

我不知道数组的最佳表示。但是为了示例,因为我无法发布实际数据本身,我需要做的是[info]数组找到每个generated_id_based_on_assication,这样我就可以从每个generated_id_based_on_assication数组中提取数据。但看到它不是一个0-n索引我不知道如何抓住“generated_id_based_on_assication”部分,所以我可以处理其中的数据,因为通过foreach或任何类型的循环运行它真的不是一个选项数组中的特定数组。有人有建议吗?如果我能够通过一个循环运行它,我可以做到,但那就是我绊倒了我不能因为这些数据被列在表中,并且所有内容都是基于每行的数组。

1 个解决方案

#1


2  

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

假设您的外部数组位于名为$ array的变量中。请尝试

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

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

然后$ keys将包含生成的ID,您可以像这样使用它们

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

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

#1


2  

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

假设您的外部数组位于名为$ array的变量中。请尝试

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

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

然后$ keys将包含生成的ID,您可以像这样使用它们

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

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