PHP的數組函數眾多,下面是我學習的小結,借此記之,便於以後鑒之……
一、數組定義:
數組的定義使用 array()方式定義,可以定義空數組:
.foreach遍歷:
復制代碼 代碼如下:
<?php
$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") //最後一句沒有標點
);
?>
二、創建數組:
創建數組包含的函數有compact()、
1.compact()函數——將一個或多個變量(包含數組)轉換為數組: array compact ( mixed $varname [, mixed $... ] )
復制代碼 代碼如下:
<?PHP
$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? ) )
2.array_combine()——將兩個數組重組成一個數組,一個作鍵值一個做的值: array array_combine ( array $keys , array $values )
復制代碼 代碼如下:
<?PHP
$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 )
3.range()函數——創建指定范圍的數組: 不多說了,直接上實例——
復制代碼 代碼如下:
<?PHP
$array1 = range(0,100,10);//0為起始值,100為結束值,10為步進值(默認步進值為1).
print_r($array1);
echo"<br />";
$array2 = range("A","Z");
print_r($array2);
echo "<br />";
$array3 = range("z","a");
print_r($array3);
?>
range()函數的默認步進值是1!
運行結果:
復制代碼 代碼如下:
Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 ) Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M [13] => N [14] => O [15] => P [16] => Q [17] => R [18] => S [19] => T [20] => U [21] => V [22] => W [23] => X [24] => Y [25] => Z ) Array ( [0] => z [1] => y [2] => x [3] => w [4] => v [5] => u [6] => t [7] => s [8] => r [9] => q [10] => p [11] => o [12] => n [13] => m [14] => l [15] => k [16] => j [17] => i [18] => h [19] => g [20] => f [21] => e [22] => d [23] => c [24] => b [25] => a )
4.array_fill()函數——填充數組函數:
復制代碼 代碼如下:
<?PHP
$array = range(1,10);
$fillarray = range("a","d");
$arrayFilled = array_fill(0,5,$fillarray);//這裡的$fillarray可以是字符串,如"test".
echo "<pre>";
print_r ($arrayFilled);
echo "</pre>";
$keys = array("string","2",9,"SDK","PK");
$array2 = array_fill_keys($keys,"testing");
echo "<pre>";
print_r ($array2);
echo "</pre>";
?>
運行結果:
復制代碼 代碼如下:
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[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
)
三、數組的遍歷:
1.foreach遍歷: foreach (array_expression as $value){}
foreach (array_expression as $key => $value){}
閒話少說,上實例:
復制代碼 代碼如下:
<?PHP
$speed = array(50,120,180,240,380);
foreach($speed as $keys=>$values){
echo $keys."=>".$values."<br />";
}
?>
運行結果:
復制代碼 代碼如下:
0=>50
1=>120
2=>180
3=>240
4=>380
2.while循環遍歷:
while循環遍歷一般結合list函數,以下是實例
復制代碼 代碼如下:
<?PHP
$staff = array(
array("姓名","性別","年齡"),
array("小張","男",24),
array("小王","女",25),
array("小李","男",23)
);
echo "<table border=2>";
while(list($keys,$value) = each($staff)){
list($name,$sex,$age) = $value;
echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";
}
echo "</table>";
?>
運行結果:
姓名
性別
年齡
小張
男
24
小王
女
25
小李
男
233.for循環遍歷:
復制代碼 代碼如下:
<?PHP
$speed = range(0,220,20);
for($i =0;$i<count($speed);$i++) {
echo $speed[$i]." ";
}
?>
運行結果:
復制代碼 代碼如下:
0 20 40 60 80 100 120 140 160 180 200 220
四、數組的指針操作:
涉及函數包括reset、prev、end、next、current、each
實例一:
復制代碼 代碼如下:
<?PHP
$speed = range(0,220,20);
echo current($speed);//輸出當前位置的值(在數組的開頭位置)
$i = rand(1,11);
while($i--){
next($speed);//指針從當前位置向後移動一位
}
echo current($speed);//輸出當前位置的值
echo "<br />";
echo prev($speed);//輸出前一位置數組值
echo "<br />";
echo reset($speed);//重置數組的指針,將指針指向起始位置
echo "<br />";
echo end($speed);//輸出最後位置的數組值
echo "<br />";
?>
運行結果:
復制代碼 代碼如下:
0220
200
0
220
實例二:each函數指針操作
復制代碼 代碼如下:
<?PHP
$speed = range(0,200,40);
echo "each實現指針下移 <br />";
echo "0擋的速度是".current(each($speed))."<br />";
echo "1擋的速度是".current(each($speed))."<br />";
echo "2擋的速度是".current(each($speed))."<br />";
echo "3擋的速度是".current(each($speed))."<br />";
echo "4擋的速度是".current(each($speed))."<br />";
echo "5擋的速度是".current(each($speed))."<br />";
echo "使用each函數實現數組指針的移動,進行數組遍歷 <br />";
reset($speed);//這裡是將數組指針指向數組首
while(list($key,$value)=each($speed)){
echo $key."=>".$value."<br />";
}
?>
運行結果:
復制代碼 代碼如下:
each實現指針下移
0擋的速度是0
1擋的速度是40
2擋的速度是80
3擋的速度是120
4擋的速度是160
5擋的速度是200
使用each函數實現數組指針的移動,進行數組遍歷
0=>0
1=>40
2=>80
3=>120
4=>160
5=>200
五、數組的增添刪改操作:
1.增添數組成員:實例一:$num[] = value直接賦值追加到數組末尾:
復制代碼 代碼如下:
<?PHP
$num = array(1=>80,2=>120,3=>160);
echo "使用表達式添加數組成員<br />";
$num[]=240;
print_r($num);
?>
運行結果:
使用表達式添加數組成員Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 240 )
實例二:array_pad函數,數組數組首尾選擇性追加
復制代碼 代碼如下:
<?PHP
$num = array(1=>80,2=>120,3=>160);
$num = array_pad($num,4,200);
echo "使用array_pad函數向數組尾部添加成員<br />";
print_r($num);
echo "<br />array_pad 還可以填充數組首部<br />";
$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):
復制代碼 代碼如下:
<?PHP
$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()在開頭添加數組成員
復制代碼 代碼如下:
<?PHP
$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開始!
2.刪減數組成員:
實例一:unset()命令刪除數組成員或數組:
復制代碼 代碼如下:
<?PHP
$num = array_fill(0,5,rand(1,10));
print_r($num);
echo "<br />";
unset($num[4]);
print_r($num);
echo "<br />";
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:\wamp\www\testing\editorplus\test.php on line 21
unset命令不能刪除整個數組
實例二:array_splice()函數刪除數組成員
復制代碼 代碼如下:
<?php
$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刪除數組中的重復值:
復制代碼 代碼如下:
<?php
$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合並數組
復制代碼 代碼如下:
<?php
$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 "<pre>";
print_r($result);
echo "</pre>";
$result = array_merge_recursive($array1,$array2,$array3,$array4,$array5);
echo "<pre>";
print_r ($result);
echo "</pre>";
?>
運行結果:
復制代碼 代碼如下:
Array
(
[r] => 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
)
)
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函數的作用是將相同字符串的鍵名單元整合成一個數組。
六、數組的鍵值和值操作:
實例一:in_array()檢測數組中是否有某個值存在
復制代碼 代碼如下:
<?php
$array = range(0,9);
if(in_array(9,$array)){
echo "數組中存在";
}
?>
運行結果:
數組中存在
實例二:key()取得數組當前的鍵名:
復制代碼 代碼如下:
<?php
$array = range(0,9);
$num = rand(0,8);
while($num--)
next($array);
$key = key($array);
echo $key;
?>
此實例結果為動態結果,范圍(0-8),不做結果演示。
實例三:list()函數把數組中的值賦給指定變量:
復制代碼 代碼如下:
<?PHP
$staff = array(
array("姓名","性別","年齡"),
array("小張","男",24),
array("小王","女",25),
array("小李","男",23)
);
echo "<table border=2>";
while(list($keys,$value) = each($staff)){
list($name,$sex,$age) = $value;
echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";
}
echo "</table>";
?>
運行結果: