2015-11-05 102 views
0

如何让儿童数组的索引数组看起来像这样PHP search_array:多维数组

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

我可以告诉你为什么不工作。在你的例子中array_column会给你一个数组内的数组,所以你的array_search甚至不会找到你正在查找的子ID。另外,即使你找到了正确的children数组,我认为array_column会返回一个从零开始的数组 – georaldc

回答

0

您可以使用此功能找到here

它不与此代码工作

我想你可以通过key(getParentStack(1009, $myarr))获得密钥或修改函数。

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;