使用php

时间:2016-06-28 07:12:11

标签: php arrays multidimensional-array

我有两个数组$ firstarray和$ secondarray如下:

[$firstarray = 0] => Array (
[id] => 1.25
[Name] => rose
[Number] => 15
[DOB] => Array (
    [0] => Array (
        [Day] => 13
        [Month] => 5
        [Year] => 1993
        )
    )
[SSN] => Array (
    [0] => 12345
    [1] => 3455
    )
)
[1] => Array (
  [id] => 1.15
  [Name] => orchid
  [Number] => 7
  [DOB] => Array (
     [0] => Array (
        [Day] => 3
        [month] => 7
        [Year] => 1912
        )
     )
 [SSN] => Array (
     [0] => 12365
     [1] => 3465
     )
 )





 $secondarray = Array ( [Day] => 13 [Month] => 5 [Year] => 1993 ) 

我想循环遍历mfirstarray,然后数组与firstarray中的每个项目相交,第二个数组。

  foreach ( $firstarray as $item ) {

      print_r(array_intersect_assoc( $secondarray, $item ); 
  }

我的问题是两个数组应该只在第一次迭代中相交,但我的代码在所有迭代中相交。我认为它只交叉密钥,即日,月,年。我怎样才能检查价值是否也匹配。有人帮助我

1 个答案:

答案 0 :(得分:1)

这就是你要找的东西

$result = array();
foreach ( $firstarray as $item ) {
    $instersectArray=array_intersect_assoc($secondarray,$item['DOB'][0]);
    if($instersectArray){
        $result[] = $item;
    }
}
print_r($result);

检查Demo

如果只需要一场比赛

$result = array();
foreach ( $firstarray as $item ) {
    $instersectArray=array_intersect_assoc($secondarray,$item['DOB'][0]);
    if($instersectArray && empty($result)){
        $result[] = $item;
    }
}
print_r($result);

检查Demo