首頁 > 後端開發 > php教程 > php常用陣列array函數實例總結【賦值,拆分,合併,計算,新增,刪除,查詢,判斷,排序】

php常用陣列array函數實例總結【賦值,拆分,合併,計算,新增,刪除,查詢,判斷,排序】

高洛峰
發布: 2023-03-04 13:08:02
原創
1649 人瀏覽過

本文實例總結了php常用陣列array函數。分享給大家供大家參考,如下:

array_combine

功能:用一個陣列的值作為新數組的鍵名,另一個數組的值作為新數組的值

案例:

<?php
$a = array("one","two","three");
$b = array("一","二","三");
$c = array_combine($a,$b);
print_r($c);
/**结果
*Array ( [one] => 一 [two] => 二 [three] => 三 )
*/
登入後複製
array_chunk

功能:分割數組成多個陣列

<?php
$input_array = array("a"=>"apple","b"=>"blue","c","d","e");
echo "<pre class="brush:php;toolbar:false">";
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2,True));
echo "
"; /**结果 Array ( [0] => Array ( [0] => apple [1] => blue ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [a] => apple [b] => blue ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [2] => e ) ) */
登入後複製

   

array_count_values

功能:統計數組中值出現的次數

功能:第一個陣列中去掉第二個陣列中有的數據,傳回剩餘的內容作為結果

<?php
$input_array = array("a"=>"apple","b"=>"blue","c","d","e");
echo "<pre class="brush:php;toolbar:false">";
print_r(array_count_values($input_array));
echo "
"; /**结果 Array ( [apple] => 1 [blue] => 1 [c] => 1 [d] => 1 [e] => 1 ) */
登入後複製

   

array_map

功能:將回呼函數執行到陣列中

<?php
$array1 = array("a"=>"apple","b"=>"blue","c","d","e");
$array2 = array("apple","c","d","f");
$result = array_diff($array1, $array2);
$result2 = array_diff($array2, $array1);
echo "<pre class="brush:php;toolbar:false">";
print_r($result);//数组1中去掉数组2中剩下的
print_r($result2);//数组2中去掉数组1中剩下的
echo "
"; /**结果 Array ( [b] => blue [2] => e ) Array ( [3] => f ) */
登入後複製
個陣列

說明:如果後面有鍵名相同的會覆寫前面的內容,鍵名為數字的會加到後面

<?php
//定义回调函数
function cube($n){
 return ($n*$n*$n);
}
$a = array(1,2,3,4,5);
$b = array_map("cube",$a);
echo "<pre class="brush:php;toolbar:false">";
print_r($b);
echo "
"; /**结果 Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 ) */
登入後複製

   

array_pop

功能:剔除數組最後一個元素,將數組剔除的元素內容

<?php
$array1 = array("color"=>"red",2,4);
$array2 = array("a","b","color"=>"green","shape"=>"trapezoid",4);
$result1 = array_merge($array1,$array2);
$result2 = array_merge_recursive($array1,$array2);
echo "<pre class="brush:php;toolbar:false">";
print_r($result1);
print_r($result2);
echo "
"; /**结果 Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) Array ( [color] => Array ( [0] => red [1] => green ) [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) */
登入後複製

   

array_push

功能:將一個多個單元壓入數組末尾,返回之後的數組個數

<?php
$stack = array("orange","banana","apple","1");
$last = array_pop($stack);
echo "<pre class="brush:php;toolbar:false">";
print_r($stack);
print_r($last);
echo "
"; /**结果 Array ( [0] => orange [1] => banana [2] => apple ) 1 */
登入後複製

   

<?php
$stack = array("orange","banana");
$count = array_push($stack,"apple","red","blue");
echo "<pre class="brush:php;toolbar:false">";
print_r($stack);
print_r($count);
echo "
"; /**结果 Array ( [0] => orange [1] => banana [2] => apple [3] => red [4] => blue ) 5 */
登入後複製

array_search

功能:查詢數組中的內容,返回鍵值,如果有多個匹配,返回第一個匹配的內容

<?php
$input = array("orange","banana","apple","red","blue");
$rand = array_rand($input,2);;
print_r($rand);
$rand = array_rand($input,3);
print_r($rand);
/**结果
Array
(
 [0] => 1
 [1] => 4
)
Array
(
 [0] => 0
 [1] => 1
 [2] => 3
)
*/
登入後複製

   

y array_pop相反

<?php
$array = array("blue"=>"b","red"=>"r","green","r");
$key = array_search(&#39;b&#39;, $array);
echo $key;
echo "<br>";
$key = array_search(&#39;r&#39;, $array);
echo $key;
echo "<br>";
/**结果
blue
red
*/
登入後複製

   

array_unique

功能:移除陣列重複的元素,保留第一個出現的,包括鍵名與值

<?php
$fruit = array("milk","orange","banana","apple");
$top = array_shift($fruit);
print_r($top);
echo "<br>";
print_r($fruit);
/**结果
milk
Array ( [0] => orange [1] => banana [2] => apple )
*/
登入後複製
部分元素

<?php
$input = array("a"=>"green","red","b"=>"green","blue","c"=>"red");
$result = array_unique($input);
print_r($result);
echo "<br>";
print_r($input);
/**结果
Array ( [a] => green [0] => red [1] => blue )
Array ( [a] => green [0] => red [b] => green [1] => blue [c] => red )
*/
登入後複製

   

count

功能:傳回陣列元素個數,元素為陣列的算一

<?php
$input = array("a","b","c","d","e");
$output = array_slice($input,2);//第二个参数没有时,表示取到最后一个元素
print_r($output);
echo "<br>";
$output = array_slice($input,-2,1);//第二个参数是正数时,表示个数;倒数第一个是-1,倒数第二个是-2
print_r($output);
echo "<br>";
$output = array_slice($input,0,3);
print_r($output);
echo "<br>";
$output = array_slice($input,2,-1);//第二个参数是负数时,表示位置,取到哪一位,不包括本身
print_r($output);
echo "<br>";
$output = array_slice($input,2,-1,true);//第三个参数为true时,保留原有的键值
print_r($output);
echo "<br>";
/**结果
Array ( [0] => c [1] => d [2] => e )
Array ( [0] => d )
Array ( [0] => a [1] => b [2] => c )
Array ( [0] => c [1] => d )
Array ( [2] => c [3] => d )
*/
登入後複製

   

in_array

功能:檢驗某值是否存在數組中,有回傳True,沒有回傳False

<?php
$input = array("a","b","c",array("d","e"));
$count = count($input);
echo $count;
echo "<br>";
$input = array("a","b","c","d","e");
$count = count($input);
echo $count;
/**结果
4
5
*/
登入後複製

   

list

函數:將數組中的資訊賦值給多個變數

list

函數

打亂陣列

<?php
$array = array("foot","bike","car","plane");
$result = current($array);
echo $result."<br>";
next($array);//使指针指向下一个元素
$result = current($array);
echo $result."<br>";
prev($array);//使指针指向前一个元素
$result = current($array);
echo $result."<br>";
end($array);//使指针指向最后一个元素
$result = current($array);
echo $result."<br>";
/**结果
foot
bike
foot
plane
*/
登入後複製

   

array_keys

功能:取得陣列的鍵名,第二個參數可以指定取得某個元素

<?php
$os_list = array("Mac","NT","Irix","Linux");
if(in_array("Irix",$os_list)){
 echo "当前操作系统列表中存在Irix";
}else{
 echo "当前操作系统列表中不存在Irix";
}
echo "<br>";
if(in_array("mac",$os_list)){
 echo "当前操作系统列表中存在mac";
}else{
 echo "当前操作系统列表中不存在mac";
}
echo "<br>";
/**结果
当前操作系统列表中存在Irix
当前操作系统列表中不存在mac
*/
登入後複製

_  的鍵名,第二個參數可以指定取得某一元素

<?php
$info = array("red","blue","green");
list($flag,$sky,$grassland) = $info;
echo "$flag,$sky,$grassland";
echo "<br>";
list($flag,,$grassland) = $info;
echo "$flag,$grassland";
echo "<br>";
list(,,$grassland) = $info;
echo "$grassland";
echo "<br>";
/**结果
red,blue,green
red,green
green
*/
登入後複製

   

arsort

功能:逆向排序,索引不變

<?php
$numbers = range(1,5);//生成一个随机数组
print_r($numbers);
echo "<br/>";
shuffle($numbers);//打乱数组
print_r($numbers);
/**结果
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Array ( [0] => 4 [1] => 1 [2] => 5 [3] => 2 [4] => 3 )
*/
登入後複製

   

sort

功能:依照鍵名進行逆向排序

<?php
$array = array(0=>100,"color"=>"red");
print_r(array_keys($array));
echo "<br>";
$array = array("blue","red","green","blue","blue");
print_r(array_keys($array,"blue"));
echo "<br>";
$array = array("color"=>array("blue","red","green"),"size"=>array("small","medium","large"));
print_r(array_keys($array));
echo "<br>";
/**结果
Array ( [0] => 0 [1] => color )
Array ( [0] => 0 [1] => 3 [2] => 4 )
Array ( [0] => color [1] => size )
*/
登入後複製

   

ksort

功能:依照鍵名進行正向排序

<?php
$input = array("php",3.0,array("green","red"));
$result = array_reverse($input); //打乱键名
$result_keyed = array_reverse($input,TRUE);//保留键名
print_r($result);
print_r($result_keyed);
/**结果
Array
(
 [0] => Array
  (
   [0] => green
   [1] => red
  )
 [1] => 3
 [2] => php
)
Array
(
 [2] => Array
  (
   [0] => green
   [1] => red
  )
 [1] => 3
 [0] => php
)
*/
登入後複製

   

sort

功能:依照數值進行正向排序,鍵名改變

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
arsort($fruits);//按照字符逆向排序或数字
foreach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**结果
b = orange
a = lemon
c = banana
d = apple
*/
登入後複製
   

希望本文所述對大家PHP程式設計有幫助。

更多php常用數組array函數實例總結【賦值,拆分,合併,計算,添加,刪除,查詢,判斷,排序】相關文章請關注PHP中文網!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板