从两个给定的数组重新创建新数组,没有重复记录

时间:2011-04-27 12:19:12

标签: php

Array1
(
    [0] => Array
        (
            [idapartment] => 212
            [guests] => 3
        )

    [1] => Array
        (
            [idapartment] => 1889
            [guests] => 6

        )

)

Array2
(
    [0] => Array
        (
            [id] => 212
            [guests] => 3

        )

    [1] => Array
        (
            [id] => 1549
            [guests] => 3

        )

    [2] => Array
        (
            [id] => 727
            [guests] => 3

        )

    [3] => Array
        (
            [id] => 1889
            [guests] => 3

        )

    [4] => Array
        (
            [id] => 1927
            [guests] => 3

        )

)

想要第三个像这个array3 :: ie删除ids 212,1889记录

Array3
(
    [0] => Array
        (
            [id] => 1927
            [guests] => 3

        )

    [1] => Array
        (
            [id] => 1549
            [guests] => 6

        )

    [2] => Array
        (
            [id] => 727 
            [guests] => 6

        )

)

1 个答案:

答案 0 :(得分:2)

foreach($array2 as $child2){
   $count = count($array1);
   $i = 0;
   for($i = 0; $i < $count ; $i++){
      if($array[$i]['idapartment'] == $child['id'])
          unset($array1[$i]);        // removes ids 212,1889 records
   }
}

$array3 = array_merge($array1, $array2);