2015-02-12 47 views
3

我有一个多维数组:递归array_search

$categories = array(
    array(
     'CategoryID' => 14308, 
     'CategoryLevel' => 1, 
     'CategoryName' => 'Alcohol & Food', 
     'CategoryParentID' => 14308 
    ), 
    // CHILD CATEGORIES 
    array(
     array(
      'CategoryID' => 179836, 
      'CategoryLevel' => 2, 
      'CategoryName' => 'Alcohol & Alcohol Mixes', 
      'CategoryParentID' => 14308 
     ), 
     array(
      array(
       'CategoryID' => 172528, 
       'CategoryLevel' => 2, 
       'CategoryName' => 'Antipasto, Savoury', 
       'CategoryParentID' => 14308 
      ) 
     ) 
    ) 
); 

我需要得到指标的准确位置,并自array_search不会对多维数组的工作,我使用的一个在PHP手册页上提供的功能。

function recursive_array_search($needle,$haystack) { 
    foreach($haystack as $key=>$value) { 
     $current_key=$key; 
     if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) { 
      return $current_key; 
     } 
    } 
    return false; 
} 

..但它也只返回第一个数组的关键:

echo recursive_array_search(172528, $categories); // outputs 1 

我期待:

[1][1][0] 

回答

5

你可以改变你的递归函数就是这样,这应该给你解决办法:

function recursive_array_search($needle, $haystack, $currentKey = '') { 
    foreach($haystack as $key=>$value) { 
     if (is_array($value)) { 
      $nextKey = recursive_array_search($needle,$value, $currentKey . '[' . $key . ']'); 
      if ($nextKey) { 
       return $nextKey; 
      } 
     } 
     else if($value==$needle) { 
      return is_numeric($key) ? $currentKey . '[' .$key . ']' : $currentKey . '["' .$key . '"]'; 
     } 
    } 
    return false; 
} 

这将导致

[1][1][0]["CategoryID"] 

由于CategoryID也是多维数组中的关键字。

如果你不希望这样,您可以将功能适应

function recursive_array_search($needle, $haystack, $currentKey = '') { 
    foreach($haystack as $key=>$value) { 
     if (is_array($value)) { 
      $nextKey = recursive_array_search($needle,$value, $currentKey . '[' . $key . ']'); 
      if ($nextKey) { 
       return $nextKey; 
      } 
     } 
     else if($value==$needle) { 
      return is_numeric($key) ? $currentKey . '[' .$key . ']' : $currentKey; 
     } 
    } 
    return false; 
} 
+2

感谢堆,这工作。对不起,这个愚蠢的问题,但如果我想打印该提取的索引的值,我不能打印'$类别。$ key',它只是打印类别,然后字面意思。 – 3zzy 2015-02-12 09:28:46

+1

我无法弄清楚这个,任何人? – 2015-07-21 13:56:53

2

你是你内心的召唤的返回值忽略到recursive_array_search。不要这样做。

/* 
* Searches for $needle in the multidimensional array $haystack. 
* 
* @param mixed $needle The item to search for 
* @param array $haystack The array to search 
* @return array|bool The indices of $needle in $haystack across the 
* various dimensions. FALSE if $needle was not found. 
*/ 
function recursive_array_search($needle,$haystack) { 
    foreach($haystack as $key=>$value) { 
     if($needle===$value) { 
      return array($key); 
     } else if (is_array($value) && $subkey = recursive_array_search($needle,$value)) { 
      array_unshift($subkey, $key); 
      return $subkey; 
     } 
    } 
} 
+0

'语法错误,' – 3zzy 2015-02-12 09:12:51

+0

中意外'返回'(T_RETURN)修复了语法错误。 – Oswald 2015-02-12 10:36:13

+0

你好。在数组中'[“name”=>“class_test”,“static”=> true,“public”=> true,“interface”=> false]'返回是'[0 =>“static”]',即只返回找到的第一个元素。回归所有这个解决方案? '[0 =>“static”,1 =>“public”]' – robertaodj 2016-08-28 13:17:41