PHP数组捕获键值对的键

时间:2012-09-11 19:43:13

标签: php arrays multidimensional-array

嗯,不确定我是否能够很好地写出来,但我会尝试。 从后端脚本我无法真正改变。我有一个非常大的多维数组向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'
                                                              )
                        )
              )
       )

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

1 个答案:

答案 0 :(得分:2)

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

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

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

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

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