• 数组的并集、交集、差集array_intersect() 和 array_diff() array_merge()函数


    array_intersect() 函数用于比较两个(或更多个)数组的键值,并返回交集

    array_intersect(array1,array2,array3...);
    
    <?php
    	$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    	$a2=array("e"=>"red","f"=>"green","g"=>"blue");
    
    	$result=array_intersect($a1,$a2);
    	print_r($result);
    ?>
    
    Array ( [a] => red [b] => green [c] => blue )
    
    

    array_diff() 函数用于比较两个(或更多个)数组的值,并返回差集。

    array_diff(array1,array2,array3...);
    
    <?php
    	$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    	$a2=array("e"=>"red","f"=>"green","g"=>"blue");
    
    	$result=array_diff($a1,$a2);
    	print_r($result);
    ?>
    
    Array ( [d] => yellow )
    

    array_merge() 函数把一个或多个数组合并为一个数组。

    array_merge(array1,array2,array3...)
    
    <?php
    	$a1=array("red","green");
    	$a2=array("blue","yellow");
    	print_r(array_merge($a1,$a2));
    ?>
    
    Array ( [0] => red [1] => green [2] => blue [3] => yellow )
    
  • 相关阅读:
    POJ2559/HDU1506 Largest Rectangle in a Histogram (cartesian tree)
    POJ2201 Cartesian Tree (cartesian tree)
    一本通1007
    一本通1006
    一本通1005
    一本通1004
    一本通1003
    一本通1002
    一本通1001
    一本通1000
  • 原文地址:https://www.cnblogs.com/caibaotimes/p/13929459.html
Copyright © 2020-2023  润新知