標籤:
1---數組中元素值的返回------->array_values();
2---數組中鍵名的返回---------->array_keys();
//這兩個函數的傳回值也同樣是數組,下表改變為索引,從0開始
<?php$arr = array(‘brand1‘=>‘lumia800‘,‘brand2‘=>‘lumia900‘,‘brand3‘=>‘lumia640xl‘); echo "<pre>"; var_dump(array_values($arr)); echo "</pre>"; echo "<hr color=red/>"; echo "<pre>"; var_dump(array_keys($arr)); echo "</pre>";
//程式的結果如下1,2
array(3) { [0]=> string(8) "lumia800" [1]=> string(8) "lumia900" [2]=> string(10) "lumia640xl"}
array(3) { [0]=> string(6) "brand1" [1]=> string(6) "brand2" [2]=> string(6) "brand3"}
//***************************************************************************************************************
3---檢查數組中是否存在某個元素---------->in_array(needle,haystach,[boll strict]);
<?php$arr=array("lumia640","lumia800","lumia1520",900);var_dump(in_array("lumia800", $arr));var_dump(in_array("900", $arr)); var_dump(in_array("900", $arr,true));//*******結果輸出**************************************//boolean true//boolean true//boolean false<-------因為第三個參數表示為嚴格尋找--->類型和值?>
3---數組元素鍵名與值得對調
<?php$arr=array("lang1" => "php", "lang2" => "python", "lang3" => "node");var_dump(array_flip($arr));//**********************//‘php‘ => string ‘lang1‘ (length=5)//‘python‘ => string ‘lang2‘ (length=5)//‘node‘ => string ‘lang3‘ (length=5)?>
4:數組元素個數的統計---count()
<?php$arr1 = array("php" =>array("php5.3","php5.4","php5.5","php7"), "python" =>array("python2.7","python3.3","python3.4") );$num1 = count($arr1); //遍曆到一維 php+python=2$num2 = count($arr1,1); //一維+二維遍曆 2+4+3=9echo ‘數組$arr1一維統計的結果是-‘.$num1."<br/>";echo ‘數組$arr1多維統計的結果是-‘.$num2."<br/>";?>
5:數組中元素出現次數的統計array_count_values()
<?php$arr=array("php",1,"python",1,"php",1);var_dump(array_count_values($arr)); // ‘php‘ => int 2 // 1 => int 3 // ‘python‘ => int 1?>
6:刪除數組中重複的值array_unique()
<?php$arr=array("a"=>"php","b"=>1,"c"=>"python","d"=>1,"e"=>"php",1);var_dump(array_unique($arr));//********如有值重複則保留前面的刪除後面的******* // ‘a‘ => string ‘php‘ (length=3) // ‘b‘ => int 1 // ‘c‘ => string ‘python‘ (length=6)?>
7:調用自訂的函數處理數組內容array_filter()
<?php$arr=array(1,2,3,4,5,6,7,8,9,10);//*************************自訂函數的調用****var_dump(array_filter($arr,"myfun"));function myfun($val){if($val%2 != 0){return $val; } }//***********輸出結果*********************** // 0 => int 1 // 2 => int 3 // 4 => int 5 // 6 => int 7 // 8 => int 9?>
8:數組元素值得排序
<?php//******************後面的元素為後加的***********$arr=array(1,4,6,2,3,‘a‘,‘g‘,‘b‘,‘A‘);sort($arr); //順序---從小到大var_dump($arr); // 0 => string ‘A‘ (length=1)----->大寫最小 // 1 => string ‘a‘ (length=1)----->小寫次之 // 2 => string ‘b‘ (length=1) // 3 => string ‘g‘ (length=1) // 4 => int 1--------------------->數字最大了 // 5 => int 2 // 6 => int 3 // 7 => int 4 // 8 => int 6rsort($arr);var_dump($arr); //倒序---從大到小// 0 => int 6// 1 => int 4// 2 => int 3// 3 => int 2// 4 => int 1?>
9: 根據條件在數組取出一段值並返回---array_slice()
<?php//下標*****0*1*2*3*4**5***6***7***8**$arr=array(1,4,6,2,3,‘a‘,‘g‘,‘b‘,‘A‘);$rest1 = array_slice($arr, 2, 3); //下標從第2位開始往後取三位$rest2 = array_slice($arr, -3, 3); //下標從倒數第2位開始往後取三位var_dump($rest1); //6,2,3var_dump($rest2); //g,b,A?>
10: 數組的合并為一個新數組,前數組鍵名,後數組索引值
<?php$arr1=array(‘os‘,‘webserver‘,‘database‘,‘script‘);$arr2=array(‘linux‘,‘apache‘,‘mongodb‘,‘php‘);//數組的合并,前面的數組元素值作為新數組鍵名,後面的數組元素值作為新數組的值var_dump(array_combine($arr1, $arr2)); // ‘os‘ => string ‘linux‘ (length=5) // ‘webserver‘ => string ‘apache‘ (length=6) // ‘database‘ => string ‘mongodb‘ (length=7) // ‘script‘ => string ‘php‘ (length=3)?>
11:數組的合并,有相同元素值的保留後一個array_merge()
<?php$arr1=array(‘a‘=>‘windows‘,‘b‘=>‘apache‘,‘c‘=>‘mysql‘);$arr2=array(‘d‘=>‘linux‘,‘b‘=>‘nginx‘,‘e‘=>‘mongodb‘);//數組的合并,若兩數組中有相同的鍵名則後面覆蓋前面var_dump(array_merge($arr1, $arr2)); // ‘a‘ => string ‘windows‘ (length=7) // ‘b‘ => string ‘nginx‘ (length=5) // ‘c‘ => string ‘mysql‘ (length=5) // ‘d‘ => string ‘linux‘ (length=5) // ‘e‘ => string ‘mongodb‘ (length=7)?>
12: 數組的交集與差集array_intersect()與array_diff()
<?php$arr1=array(‘windows‘,‘nginx‘,‘mysql‘);$arr2=array(‘linux‘,‘nginx‘,‘mongodb‘);var_dump(array_intersect($arr1, $arr2)); //nginxvar_dump(array_diff($arr1, $arr2)); //返回兩者相差的第一數組的元素// 0 => string ‘windows‘ (length=7)// 2 => string ‘mysql‘ (length=5)?>
13: 數組內容的追加--array_push()
<?php$arr1=array(‘windows‘,‘nginx‘,‘mysql‘);$arr2=array(‘linux‘,‘nginx‘,‘mongodb‘);array_push($arr1, ‘php‘); //向數組後追加一個元素array_push($arr2, $arr1); //在數組後追加一個數組var_dump($arr1); // 0 => string ‘windows‘ (length=7) // 1 => string ‘nginx‘ (length=5) // 2 => string ‘mysql‘ (length=5) // 3 => string ‘php‘ (length=3)var_dump($arr2); // 0 => string ‘linux‘ (length=5) // 1 => string ‘nginx‘ (length=5) // 2 => string ‘mongodb‘ (length=7) // 3 => // array // 0 => string ‘windows‘ (length=7) // 1 => string ‘nginx‘ (length=5) // 2 => string ‘mysql‘ (length=5) // 3 => string ‘php‘ (length=3)?>
14:數組元素由後往前刪除array_pop()
<?php$arr=array(‘linux‘,‘apache‘,‘ngnix‘,‘php‘);array_pop($arr); //刪除最後一個元素 phpvar_dump($arr);array_pop($arr); //刪除倒數第二個元素,相對於原始數組var_dump($arr);// array// 0 => string ‘linux‘ (length=5)// 1 => string ‘apache‘ (length=6)// 2 => string ‘ngnix‘ (length=5)//*********************************// array// 0 => string ‘linux‘ (length=5)// 1 => string ‘apache‘ (length=6)?>
15:數組元素由前往後刪除array_pop()
<?php$arr=array(‘linux‘,‘apache‘,‘ngnix‘,‘php‘);array_shift($arr); //刪除第一個元素 phpvar_dump($arr);array_shift($arr); //刪除第第二個元素,相對於原始數組var_dump($arr);// array// 0 => string ‘apache‘ (length=6)// 1 => string ‘ngnix‘ (length=5)// 2 => string ‘php‘ (length=3)// array// 0 => string ‘ngnix‘ (length=5)// 1 => string ‘php‘ (length=3)?>
PHP數組的一些常用函數