數組,可以說是PHP的資料應用中較重要的一種方式。 PHP的陣列函數眾多,下面是一些小結,藉此記之,方便以後鑑之。
1. 陣列定義
數組的定義使用
array()方式定義,可以定義空數組:
$number =
array(1,3,5,7,9);
//定義空數組
$result = array();
$color
=array("red","blue","green");
//自訂鍵值
$language =
(1=>"English",3=>"Chinese",5=>"Franch");
//定義二維數組
$two = array(
"color"=>array("red","blue"), //用逗號結尾
"week"=>array("Monday","Friday") //最後一句沒有標點
);
?>
2. 建立陣列
compact()
compact()函數-將一個或多個變數(包含陣列)轉換為陣列:array compact ( mixed $varname [,
mixed $... ] )。
$number = "1,3,5,7,9";
$string = "I'm PHPer";
$array =
array("And","You?");
$newArray = compact("number","string","array");
print_r ($newArray);
?>
compact()函數用於將兩個或多個變數轉換為數組,當然也包含數組變數。其參數是變數的名稱而非帶有$全名。相反的函數是extract()作用顧名思義就是將陣列轉換為單一的字串,鍵值作為其字串名稱,陣列值作為字串的值。
運行結果:
Array (
[number] => 1,3,5,7,9
[string] => I'm PHPer
[array] => Array ( [0] => And [1] =>
You? )
)
array_combine()
array_combine()-將兩個陣列重組成一個陣列,一個作鍵值一個做的值:array array_combine ( array
$keys , array $values )
$number =
array("1","3","5","7","9");
$array = array("I","Am","A","PHP","er");
$newArray = array_combine($number,$array);
print_r ($newArray);
?>
array_combine函數不多說了,誰看了都明白。
運行結果:
Array ( [1] =>
I [3] => Am [5] => A [7] => PHP [9] => er )
range()
range()函數-建立指定範圍的陣列:
$array1 =
range(0,100,10);//0為起始值,100為結束值,10為步進值(預設步進值為1).
print_r($array1);
echo"
";
$array2 = range("A","Z");
print_r($array2);
echo "
";
$array3 = range("z","a");
print_r($array3);
?>
array_fill()
array_fill()函數-填入陣列函數:
$array = range(1,10);
$fillarray = range("a","d");
$arrayFilled =
array_fill(0,5,$fillarray);//這裡的$fillarray可以是字串,如"test".
echo
"
"; <br>print_r ($arrayFilled); <br>echo "";
"; <br>print_r ($array2); <br>echo "";
[1] => Array
(
[0] => a
[1]
=> b
[2] => c
[3] => d
)
[2] => Array
(
[0] => a
[1]
=> b
[2] => c
[3] => d
)
[3] => Array
(
[0] => a
[1]
=> b
[2] => c
[3] => d
)
[4] => Array
(
[0] => a
[1]
=> b
[2] => c
[3] => d
)
)
Array
(
[string] => testing
[2] => testing
[9] => testing
[SDK] => testing
[PK]
=> testing
)
3. 陣列的遍歷
foreach遍歷
foreach
(array_expression as $value){}
foreach (array_expression as $key =>
$value){}
$speed =
array(50,120,180,240,380);
foreach($speed as $keys=>$values){
echo
$keys."=>".$values."
";
}
?>
運作結果:
0=>50
1=>120
2=>180
3=>240
4=>380
while循環遍歷
while循環遍歷一般結合list函數,以下是實例
$staff = array(
array("姓名","性別","年齡"),
array("小張","男",24),
array("小王","女",25),
array("小李","男",23)
);
echo "
$name | $sex | $age |
增添數組成員
實例一:$num[] =
value直接賦值追加到陣列結尾:
[code]$num =
array(1=>80,2=>120,3=>160);
echo "使用表達式新增陣列成員
";
$num[]=240;
print_r($num);
?>
運行結果:
使用表達式新增陣列成員
Array ( [0] => 80
[1] => 120 [2] => 160 [3] => 240 )
實例二:array_pad函數,陣列陣列首尾選擇性追加
$num =
array(1=>80,2=>120,3=>160);
$num = array_pad($num,4,200);
echo
"使用array_pad函數在陣列尾端新增成員
";
print_r($num);
echo "
array_pad 也可以填入陣列首部
";
$num = array_pad($num,-8,40);
print_r($num);
?>
運行結果:
使用array_pad函數在數組尾部添加成員
Array (
[0] => 80 [1] => 120 [2] => 160 [3] => 200 )
array_pad 也可以填入陣列首部
Array ( [0] => 40 [1] => 40 [2] => 40 [3] => 40 [4] => 80 [5]
=> 120 [6] => 160 [7] => 200 )
實例三:入棧操作追加(array_push):
$num = array(1=>80,2=>120,3=>160);
array_push($num,200,240,280);//可以自己追加,直接加在陣列結尾
print_r($num);
?>
運行結果:
Array ( [1] => 80 [2] => 120
[3] => 160 [4] => 200 [5] => 240 [6] => 280 )
實例四:array_unshift()在開頭新增陣列成員
$num =
array(1=>80,2=>120,3=>160);
array_unshift($num,0,40);//可以自己追加,直接加在陣列結尾
print_r($num);
?>
運行結果:
Array ( [0] => 0 [1] => 40 [2]
=> 80 [3] => 120 [4] => 160 )
注意:array_unshift()函數使用後數組的鍵值將會從0開始!
刪除陣列成員
實例一:unset()指令刪除陣列成員或陣列:
複製程式碼 程式碼如下:$num =
array_fill(0,5,rand(1,10));
print_r($num);
echo "
";
unset($num[4]);
print_r($num);
echo "
";
unset($num);
if(is_array){
echo "unset指令不能刪除整個陣列";
}else{
echo
"unset指令可以刪除陣列";
}
?>
運行結果:(運行出錯及說明數組也被刪除,不再存在)
Array ( [0] =>
9 [1] => 9 [2] => 9 [3] => 9 [4] => 9 )
Array ( [0] => 9 [1]
=> 9 [2] => 9 [3] => 9 )
Notice: Use of undefined constant is_array -
assumed 'is_array' in H:wampwwwtestingeditorplustest.php on line 21
unset指令不能刪除整個陣列
實例二:array_splice()函式刪除陣列成員
$a=array("red", "green", "blue", "yellow");
count ($a); //得到4
array_splice($a,1,1); //刪除第二個元素
count ($a); //得到3
echo $a[2];
//得到yellow
echo $a[1]; //得到blue
?>
實例三:array_unique刪除陣列中的重複值:
$a=array("red", "green", "blue",
"yellow","blue","green");
$result = array_unique($a);
print_r($result);
?>
運行結果:
Array ( [0] => red [1] => green [2] => blue [3]
=> yellow )
實例四:array_merge、array_merge_recursive合併陣列
$array1 = array("r"=>"red",1,2,3,4);
$array2 =
array("b"=>"blue",4=>5,6,7,8,9);
$array3 =
array("r"=>"read",4=>10,2=>11);
$array4 = array(
array(4=>10),
array(7=>13)
);
$array5 = array(
array(4=>11),
array(6=>12)
);
$result =
array_merge($array1,$array2,$array3,$array4,$array5);
echo "
"; <br>print_r($result); <br>echo "";
"; <br>print_r ($result); <br>echo "";
[12] => Array
(
[7] => 13
)
[13] => Array
(
[4] => 11
)
[14] => Array
(
[6] => 12
)
)
Array
(
[r] => Array
(
[0] => red
[1] => read
)
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[b] => blue
[4] => 5
[5] => 6
[6] =>
7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11]
=> Array
(
[4] => 10
)
[12] => Array
(
[7] => 13
)
[13] => Array
(
[4] => 11
)
[14] => Array
(
[6] => 12
)
)
註:1.
array_merge的鍵名是數字的將重新建立索引;遇到相同的字串鍵名時,後面的將覆蓋前面的。 2.
array_merge_recursive函數的作用是將相同字串的鍵名單元整合成一個陣列。
6. 陣列的鍵值和值操作
實例一:in_array()偵測數組中是否有某個值存在
$array = range(0,9);
if(in_array(9,$array)){
echo "數組中存在";
}
?>
運行結果:數組中存在
實例二:key()取得數組目前的鍵名:
$array = range(0,9);
$num =
rand(0,8);
while($num--)
next($array);
$key = key($array);
echo
$key;
?>
此實例結果為動態結果,範圍(0-8),不做結果示範。
實例三:list()函數把數組中的值賦給指定變數:
array("小李","男",23)
);
echo "
$name | $sex | $age |
實例四:array_flip()交換陣列的鍵值與值:
$array =
array("red","blue","yellow","Black");
print_r($array);
echo "
";
$array = array_flip($array);
print_r($array);
?>
運行結果:
Array ( [0] => red [1] => blue
[2] => yellow [3] => Black )
Array ( [red] => 0 [blue] => 1
[yellow] => 2 [Black] => 3 )
$array =
array("red","blue","yellow","Black");
$result = array_keys($array);
print_r($result);
echo "
";
$result =
array_values($array);
print_r($result);
?>
運行結果:
Array ( [0] => 0 [1] => 1 [2]
=> 2 [3] => 3 )
Array ( [0] => red [1] => blue [2] => yellow
[3] => Black )
實例六:array_search()搜尋數值:
$array =
array("red","blue","yellow","Black");
$result = array_search("red",$array);
if(($result === NULL)){
echo "不存在數值red";
}else{
echo "存在數值
$result";
}
?>
結果:存在數值 0
函數array_search()傳回的值可能為false或0或NULL,所以在判斷時要注意要用"==="
7. 陣列的排序
實例一:sort()、rsort()/asort()、arsort()對陣列排序:
$array = array("b","c","d","a");
sort($array);//從低到高排序
print_r($array);
echo "
";
rsort($array);//逆向排序
print_r($array);
?>
結果:
Array ( [0] => a [1] => b [2]
=> c [3] => d )
Array ( [0] => d [1] => c [2] => b [3] =>
a )
sort()、rsort()函數對陣列進行從低到高的排序,傳回結果為bool值;
asort()、arsort()函數是保留鍵值的排序,排序後鍵值不重新索引。
實例二:將陣列順序打亂-shuffle()函數:
$array = array("a","b","c","d");
shuffle($array);//從低到高排序
print_r($array);
?>
結果為動態結果:
Array ( [0] => c [1] => a
[2] => d [3] => b )
shuffle的結果有點隨機的意味,每次刷新都不一樣。
實例三:array_reverse()數組反向:
$array = array("d","b","a","c");
$array = array_reverse($array);//從低到高排序
print_r($array);
?>
運行結果:
Array ( [0] => c [1] => a [2]
=> b [3] => d )
實例四:自然排序演算法-natsort()和natcasesort();
$array =
array("sort2","Sort5","sort1","sort4");
natsort($array);//從低到高排序
print_r($array);
echo "
";
natcasesort($array);
print_r($array);
?>
結果:
Array ( [1] => Sort5 [2] => sort1
[0] => sort2 [3] => sort4 )
Array ( [2] => sort1 [0] => sort2
[3] => sort4 [1] => Sort5 )
natsort()、natcasesort()對陣列進行自然排序,就是使用數字的正常排序演算法。 natcasesort會忽略大小寫。
實例五:對陣列進行鍵值排序ksort():
$array =
array(1=>"sort2",4=>"Sort5",2=>"sort1",3=>"sort4");
ksort($array);//從低到高排序
print_r($array);
?>
結果:
Array ( [1] => sort2 [2] => sort1
[3] => sort4 [4] => Sort5 )
注意:ksort()函數重新建立了索引。
8.
數組的其他用法
cout($array) --------統計數組的單元數
array_diff($array1,$array2)----------統計數組之間的不同點,傳回第一個數組中有而第二個數組中沒有的。
array_diff_assoc($array1,$array2)---------同array_diff(),只是它對鍵值也比較
array_diff_key($array1,$array2)------------比較鍵值
array_product($array)-----------傳回陣列的所有數的乘積
array_sum($array)--------------所有數值的和
array_rand($array,$n)----------在$array數組中取出$n個數值,傳回數組
array_intersect($array1,$array2)----------------取得兩個陣列的交集
array_intersect_assoc($array1,$array2)---------------在array_intersect
的基礎上進行鍵值比較
array_intersect_key($array1,$array2)-----------------比較兩個陣列鍵值的交會
以上就介紹了php數組使用技巧及操作總結,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。