使用函數array_keys(),得到數組中所有的鍵,參數:數組
$arr=array(); $arr['one']="one"; $arr['two']="two"; $arr['three']="three"; $newArr=array_keys($arr); print_r($newArr); //Array ( [0] => one [1] => two [2] => three )
使用函數array_values(),得到數組中所有的值,參數:數組
$arr=array(); $arr[20]="one"; $arr[30]="two"; $arr[40]="three"; $newArr=array_values($arr); print_r($newArr); //Array ( [0] => one [1] => two [2] => three )
使用函數array_map(),使每個元素都調用一下自定義函數,參數:String類型函數名稱,數組
$arr=array(); $arr[0]="one"; $arr[1]="two"; $arr[2]="three"; function test($v){ return $v." Hello"; } $newArr=array_map("test",$arr); print_r($newArr); //Array ( [0] => one Hello [1] => two Hello [2] => three Hello )
使用函數array_merge(),把兩個數組合並成一個,參數:數組,數組
關聯數組合並時,鍵相同的會被後面的數組覆蓋
索引數組合並時,會連接在一起形成一個新的數組
$arr=array(); $arr[0]="one"; $arr[1]="two"; $arr[2]="three"; $arr1=array(); $arr[3]="taoshihan1"; $arr[4]="taoshihan2"; $arr[5]="taoshihan3"; $newArr=array_merge($arr,$arr1); print_r($newArr); //Array ( [0] => one [1] => two [2] => three [3] => taoshihan1 [4] => taoshihan2 [5] => taoshihan3 ) $arr=array("one","two","three"); $arr1=array("4","5","6"); $newArr=array_merge($arr,$arr1); print_r($newArr); //Array ( [0] => one [1] => two [2] => three [3] => 4 [4] => 5 [5] => 6 )
使用函數ksort(),按照鍵名進行排序,注意沒有返回新的數組,還是原來的數組
$arr=array("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"); ksort($arr); print_r($arr); //Array ( [1] => taoshihan1 [2] => taoshihan2 [3] => taoshihan3 )
使用函數array_search(),搜索某個鍵值,返回對應的鍵
$arr=array("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"); echo array_search("taoshihan1",$arr); // 1