2016-12-28 82 views
1

我已經ARRAY1這樣的:PHP array_keys多個級別

Array 
(
[0] => 123 
[1] => 456 
[2] => 789 
) 

和陣列2這樣

Array 
(

[0] => Array 
    (
     [0] => some text 
     [1] => 888 
     [2] => some 
     [3] => text 
    ) 
    [1] => Array 
    (
     [0] => some text 
     [1] => 123 
     [2] => some 
     [3] => text 
    ) 
    [2] => Array 
    (
     [0] => some text 
     [1] => 999 
     [2] => some 
     [3] => text 
    ) 
    [3] => Array 
    (
     [0] => some text 
     [1] => Array 
      (
       [1] => 456 
       [2] => 789 
      ) 
     [2] => some 
     [3] => text 
    ) 
    [4] => Array 
    (
     [0] => some text 
     [1] => 123 
     [2] => some 
     [3] => text 
    ) 
) 

我檢查僅1.第二陣列的列和尋找從第一匹配值該值陣列。這是我的代碼:

$test=array(); 

$xcol = array_column($array2, 1); 
foreach($array1 as $key => $value) { 
if(($foundKey = array_keys($xcol, $value)) !== false) { 
$rrt=$foundKey; 
foreach($rrt as $rte){ 
    $test[]=$array2[$rte]; 
} 
} 
} 

echo "<pre>"; 
print_r($test); 
echo "</pre>"; 

這是工作,給我適當的結果,但它不檢查所有級別。任何人都可以請指出我做錯了什麼? 我的輸出是:

Array 
(

    [0] => Array 
    (
     [0] => some text 
     [1] => 123 
     [2] => some 
     [3] => text 
    ) 
    [1] => Array 
    (
     [0] => some text 
     [1] => 123 
     [2] => some 
     [3] => text 
    ) 
    ) 

和期望的輸出是:

Array 
(

    [0] => Array 
    (
     [0] => some text 
     [1] => 123 
     [2] => some 
     [3] => text 
    ) 
    [1] => Array 
    (
     [0] => some text 
     [1] => Array 
      (
       [1] => 456 
       [2] => 789 
      ) 
     [2] => some 
     [3] => text 
    ) 
    [2] => Array 
    (
     [0] => some text 
     [1] => 123 
     [2] => some 
     [3] => text 
    ) 
    ) 
+0

您可以使用['recursiveArrayIterator'(http://php.net/manual/en/class.recursivearrayiterator.php) –

+0

http://php.net/manual/en/function.is-array.php – Hackerman

+0

@mister馬丁,你能給我一些提示我將如何使用比array_key搜索找到價值,仍然輸出其他所有子數組包含?我從來沒有使用recursiveArrayIterator之前,所以我有點困惑 – codexy

回答

1

解決方案:

創建一個循環,其循環您$array2持有你想獲得衛生組織值相匹配的第一個數組DATAS $array1

foreach($array2 as $data) { 

內循環創建另一個循環,其循環控制指標

foreach($data as $value) { 

但是之前創造條件,如果你的索引值是數組循環,並檢查它,它的索引值是比賽從$array1使用PHP函數in_array任何索引爲

 if (gettype($value) == "array") { 
      foreach($value as $val) { 
       if (in_array($val, $array1)) { 
        $result[] = $data; 

然後,如果你發現它只是用break避免停止循環重複

    break; 
       } 
      } 

否則你只是直接使用in_array

 } else if (in_array($value, $array1)) { 
      $result[] = $data; 
     } 
    } 
} 

這裏只是搶碼Demo

幫助它幫助只是將其標記回答如果你滿意它

1

讓我們做這個遞歸方法。什麼是你問的遞歸?那麼它只是一種稱爲自我的方法。

<?php 
$array1 = array(123,456,789); 
$array2 = array(
    array(
     "some text" 
     , 888 
     , "some" 
     , "text" 
    ), 
    array(
     "some text" 
     ,123 
     ,"some" 
     ,"text" 
    ), 
    array(
     "some text" 
     ,999 
     ,"some" 
     ,"text" 
    ), 
    array(
     "some text" 
     ,array(456,789) 
     ,"some" 
     ,"text" 
    ), 
    array(
     "another text" 
     ,123 
     ,"some" 
     ,"text" 
    ) 
); 
$final = array(); 
foreach($array1 as $needle){ 
    foreach($array2 as $haystack){ 
     if(find($needle,$haystack)){ 
      $final[] = $haystack; 
     } 
    } 
} 

print_r($final); 

function find($needle, $haystack){ 
    $result = false; 
    foreach ($haystack as $value) { 
     if(is_array($value)){ 
      $result = find($needle, $value); 
     } else { 
      if($needle == $value){ 
       $result = true; 
      } 
     } 
    } 
    return $result; 
} 
+0

我編輯我的問題與輸出也 – codexy

+0

@codexy我只是想確保你只想要頂級數組中有一個匹配的關鍵? – nerdlyist

+0

是的,匹配是3(在這個特定情況下),這就是我所需要的,所以我可以從中檢索值。 – codexy