【发布时间】:2015-11-05 00:11:47
【问题描述】:
如何获取数组的子数组的索引如下所示:
Array
(
[1000] => Array
(
[firstName] => Ori
[lastName] => Smith
[children] => Array
(
[0] => 1001
[1] => 1002
[2] => 1003
[3] => 1004
[4] => 1005
[5] => 1006
[6] => 1007
[7] => 1008
[8] => 1009
[9] => 1010
)
)
)
所以如果我给 1009 作为搜索,它应该返回 1000。
它不适用于此代码:
array_search($childrenId, array_column(myArray, 'children'));
【问题讨论】:
-
我可以告诉你为什么这不起作用。 array_column 会在您的示例中的数组中为您提供一个数组,因此您的 array_search 甚至不会找到您正在寻找的子 ID。此外,即使您确实找到了正确的子数组,我认为 array_column 也会返回一个从零开始的数组