如何获取数组的children数组的索引如下所示:
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'));
答案 0 :(得分:0)
您可以使用此功能找到here:
function getParentStack($child, $stack) {
foreach ($stack as $k => $v) {
if (is_array($v)) {
// If the current element of the array is an array, recurse it and capture the return
$return = getParentStack($child, $v);
// If the return is an array, stack it and return it
if (is_array($return)) {
return array($k => $return);
}
} else {
// Since we are not on an array, compare directly
if ($v == $child) {
// And if we match, stack it and return it
return array($k => $child);
}
}
}
// Return false since there was nothing found
return false;
}
我猜你可以通过key(getParentStack(1009, $myarr))
获取密钥或修改功能。
答案 1 :(得分:0)
试试这个
$result = array(
'1000'=>array('children'=>array('1001','1002')),
'2000'=>array('children'=>array('2001','2002'))
);
$searchValue = 2001;
$keyName = '';
foreach ($result as $key => $row) {
foreach ($row['children'] as $child) {
if ($child == $searchValue) {
$keyName = $key;
break;
}
}
}
echo $keyName;