程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php常用數組array函數實例總結【賦值,拆分,合並,計算,添加,刪除,查詢,判斷,排序】

php常用數組array函數實例總結【賦值,拆分,合並,計算,添加,刪除,查詢,判斷,排序】

編輯:PHP綜合

本文實例總結了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>";
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2,True));
echo "</pre>";
/**結果
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>";
print_r(array_count_values($input_array));
echo "</pre>";
/**結果
Array
(
 [apple] => 1
 [blue] => 1
 [c] => 1
 [d] => 1
 [e] => 1
)
*/

array_diff

功能:第一個數組中去掉第二個數組中有的數據,返回剩下的內容作為結果

<?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>";
print_r($result);//數組1中去掉數組2中剩下的
print_r($result2);//數組2中去掉數組1中剩下的
echo "</pre>";
/**結果
Array
(
 [b] => blue
 [2] => e
)
Array
(
 [3] => f
)
*/

array_map

功能:將回調函數執行到數組中

<?php
//定義回調函數
function cube($n){
 return ($n*$n*$n);
}
$a = array(1,2,3,4,5);
$b = array_map("cube",$a);
echo "<pre>";
print_r($b);
echo "</pre>";
/**結果
Array
(
 [0] => 1
 [1] => 8
 [2] => 27
 [3] => 64
 [4] => 125
)
*/

array_merge

功能:合並一個或多個數組

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

<?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>";
print_r($result1);
print_r($result2);
echo "</pre>";
/**結果
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_pop

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

<?php
$stack = array("orange","banana","apple","1");
$last = array_pop($stack);
echo "<pre>";
print_r($stack);
print_r($last);
echo "</pre>";
/**結果
Array
(
 [0] => orange
 [1] => banana
 [2] => apple
)
1
*/

array_push

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

<?php
$stack = array("orange","banana");
$count = array_push($stack,"apple","red","blue");
echo "<pre>";
print_r($stack);
print_r($count);
echo "</pre>";
/**結果
Array
(
 [0] => orange
 [1] => banana
 [2] => apple
 [3] => red
 [4] => blue
)
5
*/

array_rand

功能:獲取隨機的鍵名

<?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
)
*/

array_search

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

<?php
$array = array("blue"=>"b","red"=>"r","green","r");
$key = array_search('b', $array);
echo $key;
echo "<br>";
$key = array_search('r', $array);
echo $key;
echo "<br>";
/**結果
blue
red
*/

array_shift

功能:移除開頭的元素,與array_pop相反

<?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 )
*/

array_unique

功能:去除數組重復的元素,保留第一個出現的,包括鍵名和值

<?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 )
*/

array_slice

功能:從數組中取出部分元素

<?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 )
*/

count

功能:返回數組元素個數,元素為數組的算一個

<?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
*/

current

功能:獲取當前的指針指向元素

<?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
*/

in_array

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

<?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
*/

list

功能:將數組中的信息賦值給多個變量

<?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
*/

shuffle

功能:打亂數組

<?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 )
*/

array_keys

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

<?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 )
*/

array_reverse

功能:獲取數組的反向

<?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
)
*/

arsort

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

<?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
*/

asort

功能:進行正向排序

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
arsort($fruits);//按照字符逆向排序或數字
foreach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
echo "<p>";
asort($fruits);//按照字符正向排序或數字
foreach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**結果
b = orange
a = lemon
c = banana
d = apple
d = apple
c = banana
a = lemon
b = orange
*/

krsort

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

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
krsort($fruits);//按照鍵名逆向排序或數字
foreach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**結果
d = apple
c = banana
b = orange
a = lemon
*/

ksort

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

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
ksort($fruits);//按照鍵名正向排序或數字
foreach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**結果
a = lemon
b = orange
c = banana
d = apple
*/

rsort

功能:按照值進行逆向排序,鍵名改變

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
rsort($fruits);//按照值進行逆向排序或數字,鍵名改變
foreach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**結果
0 = orange
1 = lemon
2 = banana
3 = apple
*/

sort

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

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
sort($fruits);//按照值進行逆向排序或數字,鍵名改變
foreach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**結果
0 = apple
1 = banana
2 = lemon
3 = orange
*/

更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php常用函數與技巧總結》、《PHP錯誤與異常處理方法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved