php array数组(第二部分)

array_fill() 用给定的键值填充数组

参数 index 必须。规定返回数组的起始索引。
参数 number 必须。规定填充的元素的数量,其值必须大于0。
参数 value 必须。规定用于填充数组的键值。

<?php
    $a1=array_fill(3,4,"blue");
    $b1=array_fill(0,1,"red");
    print_r($a1);
    echo "<br>";
    print_r($b1);
?>
//输出
Array ( [3] => blue [4] => blue [5] => blue [6] => blue ) 
Array ( [0] => red )
array_fill_keys() 用给定的指定键名的键值填充数组
<?php
    $keys=array("a","b","c","d");
    $a1=array_fill_keys($keys,"blue");
    print_r($a1);
?>
//输出
Array ( [a] => blue [b] => blue [c] => blue [d] => blue )
array_filter() 用回调函数过滤数组中的元素

该函数把输入数组中的每个键值传给回调函数。如果回调函数返回true,
则把输入数组中的当前键值返回给结果数组。数组键名保持不变。

<?php
    function test_odd($var)
    {
    return($var & 1);
    }
    
    $a1=array("a","b",2,3,4);
    print_r(array_filter($a1,"test_odd"));
?>
//输出
Array ( [3] => 3 )
array_flip()

反转/交换数组中的键名和对应关联的键值

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    $result=array_flip($a1);
    print_r($result);
?>
//输出
Array ( [red] => a [green] => b [blue] => c [yellow] => d )
array_intersect()

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

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

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    $a2=array("e"=>"red","f"=>"black","g"=>"purple");
    $a3=array("a"=>"red","b"=>"black","h"=>"yellow");
    
    $result=array_intersect($a1,$a2,$a3);
    print_r($result);
?>
//输出
Array ( [a] => red )
array_intersect_assoc()

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

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    $a2=array("a"=>"red","b"=>"green","c"=>"blue");
    
    $result=array_intersect_assoc($a1,$a2);
    print_r($result);
?>
//输出
Array ( [a] => red [b] => green [c] => blue )

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    $a2=array("a"=>"red","b"=>"green","g"=>"blue");
    $a3=array("a"=>"red","b"=>"green","g"=>"blue");
    
    $result=array_intersect_assoc($a1,$a2,$a3);
    print_r($result);
?>
//输出
Array ( [a] => red [b] => green )
array_intersect_key()

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

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue");
    $a2=array("a"=>"red","c"=>"blue","d"=>"pink");
    
    $result=array_intersect_key($a1,$a2);
    print_r($result);
?>
//输出
Array ( [a] => red [c] => blue )

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue");
    $a2=array("c"=>"yellow","d"=>"black","e"=>"brown");
    $a3=array("f"=>"green","c"=>"purple","g"=>"red");
    
    $result=array_intersect_key($a1,$a2,$a3);
    print_r($result);
?>
//输出
Array ( [c] => blue )
array_intersect_uassoc()

比较两个(或更多)数组的键名和键值(使用用户自定义函数比较键名),并返回交集.

<?php
    function myfunction($a,$b)
    {
    if ($a===$b)
       {
       return 0;
       }
       return ($a>$b)?1:-1;
    }
    
    $a1=array("a"=>"red","b"=>"green","c"=>"blue");
    $a2=array("d"=>"red","b"=>"green","e"=>"blue");
    
    $result=array_intersect_uassoc($a1,$a2,"myfunction");
    print_r($result);
?>
//输出
Array ( [b] => green )
array_intersect_ukey()

比较两个(或更多)数组(使用用户自定义函数比较键名),并返回交集。

<?php
    function myfunction($a,$b)
    {
    if ($a===$b)
       {
       return 0;
       }
       return ($a>$b)?1:-1;
    }
    
    $a1=array("a"=>"red","b"=>"green","c"=>"blue");
    $a2=array("a"=>"blue","b"=>"black","e"=>"blue");
    
    $result=array_intersect_ukey($a1,$a2,"myfunction");
    print_r($result);
?>
//输出
Array ( [a] => red [b] => green )
posted @ 2017-04-22 10:52  Yxh_blogs  阅读(188)  评论(0编辑  收藏  举报