2013-07-12 123 views
0

我有一個數組$ arr。如何從多維數組中獲取父數組索引php

當我打印它時,它顯示如下結果。

Array 
(
    [0] => Array 
     (
      [name] => homeandgarden-Control Type 
      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [1] => Array 
     (
      [name] => homeandgarden-Depth 
      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [2] => Array 
     (
      [name] => homeandgarden-Height 
      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [3] => Array 
     (
      [name] => homeandgarden-Load Type 
      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [4] => Array 
     (
      [name] => homeandgarden-Machine Type 
      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [5] => Array 
     (
      [name] => homeandgarden-Type 
      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [6] => Array 
     (
      [name] => homeandgarden-Width 
      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [7] => Array 
     (
      [name] => computer & ele 
      [label] => 
      [singular_label] => 
      [hierarchical] => 1 
      [show_ui] => 1 
      [query_var] => 1 
      [rewrite] => 1 
      [rewrite_slug] => 
      [0] => Array 
       (
        [search_items] => 
        [popular_items] => 
        [all_items] => 
        [parent_item] => 
        [parent_item_colon] => 
        [edit_item] => 
        [update_item] => 
        [add_new_item] => 
        [new_item_name] => 
        [separate_items_with_commas] => 
        [add_or_remove_items] => 
        [choose_from_most_used] => 
       ) 

      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [8] => Array 
     (
      [name] => computer 
      [label] => 
      [singular_label] => 
      [hierarchical] => 1 
      [show_ui] => 1 
      [query_var] => 1 
      [rewrite] => 1 
      [rewrite_slug] => 
      [0] => Array 
       (
        [search_items] => 
        [popular_items] => 
        [all_items] => 
        [parent_item] => 
        [parent_item_colon] => 
        [edit_item] => 
        [update_item] => 
        [add_new_item] => 
        [new_item_name] => 
        [separate_items_with_commas] => 
        [add_or_remove_items] => 
        [choose_from_most_used] => 
       ) 

      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [9] => Array 
     (
      [name] => homeandgardenairconditioner-Type 
      [label] => 
      [singular_label] => 
      [hierarchical] => 1 
      [show_ui] => 1 
      [query_var] => 1 
      [rewrite] => 1 
      [rewrite_slug] => 
      [0] => Array 
       (
        [search_items] => 
        [popular_items] => 
        [all_items] => 
        [parent_item] => 
        [parent_item_colon] => 
        [edit_item] => 
        [update_item] => 
        [add_new_item] => 
        [new_item_name] => 
        [separate_items_with_commas] => 
        [add_or_remove_items] => 
        [choose_from_most_used] => 
       ) 

      [1] => Array 
       (
        [0] => product 
       ) 

     ) 

    [10] => Array 
     (
      [name] => homeandgardendishwasher-Control Type 
      [label] => 
      [singular_label] => 
      [hierarchical] => 1 
      [show_ui] => 1 
      [query_var] => 1 
      [rewrite] => 1 
      [rewrite_slug] => 
      [0] => Array 
       (
        [search_items] => 
        [popular_items] => 
        [all_items] => 
        [parent_item] => 
        [parent_item_colon] => 
        [edit_item] => 
        [update_item] => 
        [add_new_item] => 
        [new_item_name] => 
        [separate_items_with_commas] => 
        [add_or_remove_items] => 
        [choose_from_most_used] => 
       ) 

      [1] => Array 
       (
        [0] => product 
       ) 

     ) 
) 

,我有一個變量$ categ.For例如$ CATEG =「homeandgardenairconditioner型」 那麼我想知道他的「」父數組的索引是「homeandgardenairconditioner-鍵入

即--- O/p應爲9

如何得到這個指標。請幫我

感謝

+0

@PLB我不知道如何遍歷這個數組,所以我首先檢查它的名字,然後得到它的父數組索引th at爲什麼我問。謝謝 –

+0

它是否已經定義您的值將位於數組的第一維?或者它應該用於在'$ array [$ i] [$ j] [$ k] [$ etc]''中查找值? –

+0

@ Frederik.L我有'名稱'。你可以寫你的代碼嗎?謝謝 –

回答

7

可能重複,我認爲你正在尋找這樣的:

function getIndex($name, $array){ 
    foreach($array as $key => $value){ 
     if(is_array($value) && $value['name'] == $name) 
       return $key; 
    } 
    return null; 
} 
+0

是的考拉。我得到了這個謝謝 –

0

試試這個,

function find_parent($array, $needle, $parent = null) { 
    foreach ($array as $key => $value) { 
     if (is_array($value)) { 
      $pass = $parent; 
      if (is_string($key)) { 
       $pass = $key; 
      } 
      $found = find_parent($value, $needle, $pass); 
      if ($found !== false) { 
       return $found; 
      } 
     } else if ($key === 'id' && $value === $needle) { 
      return $parent; 
     } 
    } 

    return false; 
} 

$parentkey = find_parent($array, '0002'); 

PHP - Find parent key of array

+0

它沒有給我解決方案。我的情況是littile不同。我不搜索另一個陣列,而我只是想匹配「名稱」 –

+0

複製答案從https://stackoverflow.com/a/2504778/6556397複製 – rahulsm