標籤:返回結果 merge world uniq txt 數組 數組函數 索引 imp
排序:
sort() 函數用於對數組單元從低到高進行排序。
rsort() 函數用於對數組單元從高到低進行排序。
asort() 函數用於對數組單元從低到高進行排序並保持索引關係。
arsort() 函數用於對數組單元從高到低進行排序並保持索引關係。
ksort() 函數用於對數組單元按照鍵名從低到高進行排序。
krsort() 函數用於對數組單元按照鍵名從高到低進行排序。
去重:
array_unique() 函數移除數組中的重複的值,並返回結果數組。
<?php $a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat"); print_r(array_unique($a)); ?> 輸出: Array ( [a] => Cat [b] => Dog )
並集:array_merge();//並集
// $a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
// $a1=array("a"=>"Cat2","b"=>"Dog3","c"=>"Cat4");
// $result=array_merge($a1,$a);
// print_r($result);
array_intersect()//交集
$a=array(1,2,3,6);$b=array(2,4,6,8);$result=array_intersect($b,$a);print_r($result);輸出:Array ( [0] => 2 [2] => 6 )
array_diff() 求差集
$old = array(‘jpg‘,‘png‘,‘gif‘,‘bmp‘); $new = array(‘jpg‘,‘txt‘,‘docx‘,‘bmp‘); $difference = array_diff($old, $new); //在old數組中,不在new數組中print_r($difference);
數組轉字串
$arr = array(‘Hello‘,‘World!‘,‘I‘,‘love‘,‘Shanghai!‘);
echo implode(" ",$arr);
輸出 Hello World! I love Shanghai!
字串轉數組
$str="1432532 ,4444";
$arr = explode(",",$str);
print_r($arr);
輸出:Array ( [0] => 1432532 [1] => 4444 )
php 數組函數