PHPは配列の交差と差セットを得る

13406 ワード

PHP          

  :              :2011-08-26 11:30:21 38603    

         218.642 ms   6      ,               。

      array_intersect()

array_intersect()             ,                                 。     :

1    array array_intersect(array array1,array array2[,arrayN…])
          $fruit1        $fruit2 $fruit3          :

01    <?php
02    $fruit1 = array("Apple","Banana","Orange");
03    $fruit2 = array("Pear","Apple","Grape");
04    $fruit3 = array("Watermelon","Orange","Apple");
05    $intersection = array_intersect($fruit1, $fruit2, $fruit3);
06    print_r($intersection);
07     
08    // output
09    // Array ( [0] => Apple )
10    ?>
                    ,array_intersect()            。

        array_intersect_assoc()

  array_intersect_assoc() array_intersect()    ,                。  ,           ,                /           。

    :

1    array array_intersect_assoc(array array1,array array2[,arrayN…])
           $fruit1   ,      $fruit2 $fruit3     /01    <?php
02    $fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");
03    $fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");
04    $fruit3 =array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");
05    $intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3);
06    print_r($intersection);
07     
08    // output
09    // Array ( [red] => Apple )
10    ?>
      array_diff()

  array_diff()                       。     array_intersect()  。

1    array array_diff(array array1,array array2[,arrayN…])
    :

01    <?php
02    $fruit1 = array("Apple","Banana","Orange");
03    $fruit2 = array("Pear","Apple","Grape");
04    $fruit3 = array("Watermelon","Orange","Apple");
05    $intersection = array_diff($fruit1, $fruit2, $fruit3);
06    print_r($intersection);
07     
08    // output
09    // Array ( [1] => Banana )
10    ?>
        array_diff_assoc()

  array_diff_assoc() array_diff()    ,               。  ,                        /            。     :

1    array array_diff_assoc(array array1,array array2[,arrayN…])
         [yellow] => Banana,        /     $fruit1 ,  $fruit2 $fruit3     。

01    <?php
02    $fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");
03    $fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");
04    $fruit3 =array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");
05    $intersection = array_diff_assoc($fruit1, $fruit2, $fruit3);
06    print_r($intersection);
07     
08    // output
09    // Array ( [yellow] => Banana )
10    ?>