//建立一個陣列 $arrays = array("name"=>"Job","John","Tom","John",5,2); //初始狀態 print_r($arrays); //Array ( [name] => Job [0] => John [1] => Tom [2] => John [3] => 5 [4] => 2 ) //is_array(array) 回傳變數是否為陣列 echo is_array($arrays) ? '$arrays是陣列' : '$arrays不是陣列'; //$arrays是陣列 //array_key_exists(str,array) 回傳變數值是否為陣列裡的索引值 echo array_key_exists('name',$arrays) ? '索引name存在' : '索引name不存在'; //索引name存在 echo array_key_exists(3,$arrays) ? '索引3存在' : '索引3不存在'; //索引3存在 //in_array(str,array) 回傳變數值是否為陣列裡的數值 echo in_array('Job',$arrays) ? 'Job存在' : 'Job不存在'; //Job存在 //count(array) 回傳陣列長度 echo count($arrays); //6 //array_values(array) 回傳數字索引的陣列 print_r(array_values($arrays)); //Array ( [0] => Job [1] => John [2] => Tom [3] => John [4] => 5 [5] => 2 ) //array_count_values(array) 回傳重覆元素的次數的陣列 print_r(array_count_values($arrays)); //Array ( [Job] => 1 [John] => 2 [Tom] => 1 [5] => 1 [2] => 1 ) //array_reverse(array) 回傳反轉後的順序的陣列 print_r(array_reverse($arrays)); //Array ( [0] => 2 [1] => 5 [2] => John [3] => Tom [4] => John [name] => Job ) //array_pad(array,length,value) 回傳填充陣列長度 print_r(array_pad($arrays,10,'Mary')); //Array ( [name] => Job [0] => John [1] => Tom [2] => John [3] => 5 [4] => 2 [5] => Mary [6] => Mary [7] => Mary [8] => Mary ) ※蘭さん:原本的陣列長度是6,這裡設定10個,那麼就是要再補上4個Mary。 //array_sum(array) 回傳總和(字串忽略) echo array_sum($arrays); //7 //current(array) 回傳目前指標位置的數值 echo current($arrays); //Job //array_unique(array) 回傳刪除陣列的重覆值 print_r(array_unique($arrays)); //Array ( [name] => Job [0] => John [1] => Tom [3] => 5 [4] => 2 ) //shuffle($arrays) 不回傳,將陣列的所有元素打亂 shuffle($arrays); print_r($arrays); //Array ( [0] => Tom [1] => 5 [2] => Job [3] => John [4] => 2 [5] => John )