PHP array_keys多个级别

时间:2016-12-28 14:18:38

标签: php multidimensional-array

我有这样的array1:

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
    )   
  )

我只检查第二个数组的第一列,并查找与第一个数组的值匹配的值。这是我的代码:

 $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
    )   
   )

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

解决方案:

创建一个循环,循环你的$array2,它包含你想要得到的数据,它们在第一个数组中匹配值$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

中的代码即可

如果您对此感到满意,请帮助它标记答案