2016-12-28 61 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