一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

php 數(shù)組使用詳解 推薦

php的數(shù)組函數(shù)眾多,下面是我學(xué)習(xí)的小結(jié),借此記之,便于以后鑒之……
  一、數(shù)組定義:
  數(shù)組的定義使用 array()方式定義,可以定義空數(shù)組:
.foreach遍歷:
復(fù)制代碼 代碼如下:
<?php
$number = array(1,3,5,7,9);
//定義空數(shù)組
$result = array();
$color =array("red","blue","green");
//自定義鍵值
$language = (1=>"English",3=>"Chinese",5=>"Franch");
//定義二維數(shù)組
$two = array(
"color"=>array("red","blue"), //用逗號(hào)結(jié)尾
"week"=>array("Monday","Friday") //最后一句沒有標(biāo)點(diǎn)
);
?>

二、創(chuàng)建數(shù)組:
  創(chuàng)建數(shù)組包含的函數(shù)有compact()、
    1.compact()函數(shù)――將一個(gè)或多個(gè)變量(包含數(shù)組)轉(zhuǎn)換為數(shù)組:      array compact ( mixed $varname [, mixed $... ] )  
復(fù)制代碼 代碼如下:
<?php
$number = "1,3,5,7,9";
$string = "I'm phper";
$array = array("And","You?");
$newArray = compact("number","string","array");
print_r ($newArray);
?>

compact()函數(shù)用于將兩個(gè)或多個(gè)變量轉(zhuǎn)換為數(shù)組,當(dāng)然也包含數(shù)組變量。其參數(shù)是變量的名稱而非帶有$全名。
      相反的函數(shù)是extract()作用顧名思義就是將數(shù)組轉(zhuǎn)換為單個(gè)的字符串,鍵值作為其字符串名稱,數(shù)組值作為字符串的值。
      運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
Array ( [number] => 1,3,5,7,9 [string] => I'm phper [array] => Array ( [0] => And [1] => You? ) )

2.array_combine()――將兩個(gè)數(shù)組重組成一個(gè)數(shù)組,一個(gè)作鍵值一個(gè)做的值:      array array_combine ( array $keys , array $values )
復(fù)制代碼 代碼如下:
<?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函數(shù)不多說了,誰看了都明白
      運(yùn)行結(jié)果:
Array ( [1] => I [3] => Am [5] => A [7] => php [9] => er )
    3.range()函數(shù)――創(chuàng)建指定范圍的數(shù)組:       不多說了,直接上實(shí)例――
復(fù)制代碼 代碼如下:
<?php
$array1 = range(0,100,10);//0為起始值,100為結(jié)束值,10為步進(jìn)值(默認(rèn)步進(jìn)值為1).
print_r($array1);
echo"<br />";
$array2 = range("A","Z");
print_r($array2);
echo "<br />";
$array3 = range("z","a");
print_r($array3);
?>

range()函數(shù)的默認(rèn)步進(jìn)值是1!
      運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
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()函數(shù)――填充數(shù)組函數(shù):
復(fù)制代碼 代碼如下:
<?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>";
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
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
)

三、數(shù)組的遍歷:
    1.foreach遍歷:       foreach (array_expression as $value){}
       foreach (array_expression as $key => $value){}
      閑話少說,上實(shí)例:
復(fù)制代碼 代碼如下:
<?php
$speed = array(50,120,180,240,380);
foreach($speed as $keys=>$values){
echo $keys."=>".$values."<br />";
}
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
0=>50
1=>120
2=>180
3=>240
4=>380

2.while循環(huán)遍歷:
while循環(huán)遍歷一般結(jié)合list函數(shù),以下是實(shí)例
復(fù)制代碼 代碼如下:
<?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>";
?>

運(yùn)行結(jié)果:
姓名性別年齡
小張24
小王25
小李23
3.for循環(huán)遍歷:
復(fù)制代碼 代碼如下:
<?php
$speed = range(0,220,20);
for($i =0;$i<count($speed);$i++) {
echo $speed[$i]." ";
}
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
0 20 40 60 80 100 120 140 160 180 200 220

四、數(shù)組的指針操作:
    涉及函數(shù)包括reset、prev、end、next、current、each
實(shí)例一:
復(fù)制代碼 代碼如下:
<?php
$speed = range(0,220,20);
echo current($speed);//輸出當(dāng)前位置的值(在數(shù)組的開頭位置)
$i = rand(1,11);
while($i--){
next($speed);//指針從當(dāng)前位置向后移動(dòng)一位
}
echo current($speed);//輸出當(dāng)前位置的值
echo "<br />";
echo prev($speed);//輸出前一位置數(shù)組值
echo "<br />";
echo reset($speed);//重置數(shù)組的指針,將指針指向起始位置
echo "<br />";
echo end($speed);//輸出最后位置的數(shù)組值
echo "<br />";
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
0220
200
0
220

實(shí)例二:each函數(shù)指針操作
復(fù)制代碼 代碼如下:
<?php
$speed = range(0,200,40);
echo "each實(shí)現(xiàn)指針下移 <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函數(shù)實(shí)現(xiàn)數(shù)組指針的移動(dòng),進(jìn)行數(shù)組遍歷 <br />";
reset($speed);//這里是將數(shù)組指針指向數(shù)組首
while(list($key,$value)=each($speed)){
echo $key."=>".$value."<br />";
}
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
each實(shí)現(xiàn)指針下移
0擋的速度是0
1擋的速度是40
2擋的速度是80
3擋的速度是120
4擋的速度是160
5擋的速度是200
使用each函數(shù)實(shí)現(xiàn)數(shù)組指針的移動(dòng),進(jìn)行數(shù)組遍歷
0=>0
1=>40
2=>80
3=>120
4=>160
5=>200

五、數(shù)組的增添刪改操作:
    1.增添數(shù)組成員:實(shí)例一:$num[] = value直接賦值追加到數(shù)組末尾:
復(fù)制代碼 代碼如下:
<?php
$num = array(1=>80,2=>120,3=>160);
echo "使用表達(dá)式添加數(shù)組成員<br />";
$num[]=240;
print_r($num);
?>

運(yùn)行結(jié)果:
使用表達(dá)式添加數(shù)組成員Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 240 )
實(shí)例二:array_pad函數(shù),數(shù)組數(shù)組首尾選擇性追加
復(fù)制代碼 代碼如下:
<?php
$num = array(1=>80,2=>120,3=>160);
$num = array_pad($num,4,200);
echo "使用array_pad函數(shù)向數(shù)組尾部添加成員<br />";
print_r($num);
echo "<br />array_pad 還可以填充數(shù)組首部<br />";
$num = array_pad($num,-8,40);
print_r($num);
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
使用array_pad函數(shù)向數(shù)組尾部添加成員Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 200 ) array_pad 還可以填充數(shù)組首部Array ( [0] => 40 [1] => 40 [2] => 40 [3] => 40 [4] => 80 [5] => 120 [6] => 160 [7] => 200 )

實(shí)例三:入棧操作追加(array_push):
復(fù)制代碼 代碼如下:
<?php
$num = array(1=>80,2=>120,3=>160);
array_push($num,200,240,280);//可以自己追加,直接加在數(shù)組結(jié)尾
print_r($num);
?>

運(yùn)行結(jié)果:
Array ( [1] => 80 [2] => 120 [3] => 160 [4] => 200 [5] => 240 [6] => 280 )
實(shí)例四:array_unshift()在開頭添加數(shù)組成員
復(fù)制代碼 代碼如下:
<?php
$num = array(1=>80,2=>120,3=>160);
array_unshift($num,0,40);//可以自己追加,直接加在數(shù)組結(jié)尾
print_r($num);
?>

運(yùn)行結(jié)果:
Array ( [0] => 0 [1] => 40 [2] => 80 [3] => 120 [4] => 160 )
注意:array_unshift()函數(shù)使用后數(shù)組的鍵值將會(huì)從0開始!
     2.刪減數(shù)組成員:
實(shí)例一:unset()命令刪除數(shù)組成員或數(shù)組:
復(fù)制代碼 代碼如下:
<?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命令不能刪除整個(gè)數(shù)組";
}else{
echo "unset命令可以刪除數(shù)組";
}
?>

運(yùn)行結(jié)果:(運(yùn)行出錯(cuò)及說明數(shù)組也被刪除,不再存在)
復(fù)制代碼 代碼如下:
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命令不能刪除整個(gè)數(shù)組

實(shí)例二:array_splice()函數(shù)刪除數(shù)組成員
復(fù)制代碼 代碼如下:
<?php
$a=array("red", "green", "blue", "yellow");
count ($a); //得到4
array_splice($a,1,1); //刪除第二個(gè)元素
count ($a); //得到3
echo $a[2]; //得到y(tǒng)ellow
echo $a[1]; //得到blue
?>

實(shí)例三:array_unique刪除數(shù)組中的重復(fù)值:
復(fù)制代碼 代碼如下:
<?php
$a=array("red", "green", "blue", "yellow","blue","green");
$result = array_unique($a);
print_r($result);
?>

運(yùn)行結(jié)果:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
實(shí)例四:array_merge、array_merge_recursive合并數(shù)組
復(fù)制代碼 代碼如下:
<?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>";
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
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的鍵名是數(shù)字的將重新建立索引;遇到相同的字符串鍵名時(shí),后面的將覆蓋前面的。
  2.array_merge_recursive函數(shù)的作用是將相同字符串的鍵名單元整合成一個(gè)數(shù)組。
  六、數(shù)組的鍵值和值操作:
實(shí)例一:in_array()檢測(cè)數(shù)組中是否有某個(gè)值存在
復(fù)制代碼 代碼如下:
<?php
$array = range(0,9);
if(in_array(9,$array)){
echo "數(shù)組中存在";
}
?>

運(yùn)行結(jié)果:
數(shù)組中存在
實(shí)例二:key()取得數(shù)組當(dāng)前的鍵名:
復(fù)制代碼 代碼如下:
<?php
$array = range(0,9);
$num = rand(0,8);
while($num--)
next($array);
$key = key($array);
echo $key;
?>

此實(shí)例結(jié)果為動(dòng)態(tài)結(jié)果,范圍(0-8),不做結(jié)果演示。
實(shí)例三:list()函數(shù)把數(shù)組中的值賦給指定變量:
復(fù)制代碼 代碼如下:
<?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>";
?>

運(yùn)行結(jié)果:


實(shí)例四:array_flip()交換數(shù)組的鍵值和值:
復(fù)制代碼 代碼如下:
<?php
$array = array("red","blue","yellow","Black");
print_r($array);
echo "<br />";
$array = array_flip($array);
print_r($array);
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
Array ( [0] => red [1] => blue [2] => yellow [3] => Black )
Array ( [red] => 0 [blue] => 1 [yellow] => 2 [Black] => 3 )

實(shí)例五:array_keys()、array_values()返回?cái)?shù)組中所有的鍵值和值:
復(fù)制代碼 代碼如下:
<?php
$array = array("red","blue","yellow","Black");
$result = array_keys($array);
print_r($result);
echo "<br />";
$result = array_values($array);
print_r($result);
?>

運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
Array ( [0] => red [1] => blue [2] => yellow [3] => Black )

實(shí)例六:array_search()搜索數(shù)值:
復(fù)制代碼 代碼如下:
<?php
$array = array("red","blue","yellow","Black");
$result = array_search("red",$array);
if(($result === NULL)){
echo "不存在數(shù)值red";
}else{
echo "存在數(shù)值 $result";
}
?>

結(jié)果:存在數(shù)值 0
函數(shù)array_search()返回的值可能為false或0或NULL,所以在判斷時(shí)注意要用"==="
  七、數(shù)組的排序:
實(shí)例一:sort()、rsort()/asort()、arsort()對(duì)數(shù)組排序:
復(fù)制代碼 代碼如下:
<?php
$array = array("b","c","d","a");
sort($array);//從低到高排序
print_r($array);
echo "<br />";
rsort($array);//逆向排序
print_r($array);
?>

結(jié)果:
Array ( [0] => a [1] => b [2] => c [3] => d )
Array ( [0] => d [1] => c [2] => b [3] => a )
sort()、rsort()函數(shù)對(duì)數(shù)組進(jìn)行從低到高的排序,返回結(jié)果為bool值;
asort()、arsort()函數(shù)是保留鍵值的排序,排序后鍵值不重新索引。
實(shí)例二:將數(shù)組順序打亂――shuffle()函數(shù):
復(fù)制代碼 代碼如下:
<?php
$array = array("a","b","c","d");
shuffle($array);//從低到高排序
print_r($array);
?>

結(jié)果為動(dòng)態(tài)結(jié)果:
Array ( [0] => c [1] => a [2] => d [3] => b )
shuffle的結(jié)果有點(diǎn)隨機(jī)的意味,每次刷新都不一樣。

實(shí)例三:array_reverse()數(shù)組反向:
復(fù)制代碼 代碼如下:
<?php
$array = array("d","b","a","c");
$array = array_reverse($array);//從低到高排序
print_r($array);
?>

運(yùn)行結(jié)果:
Array ( [0] => c [1] => a [2] => b [3] => d )
實(shí)例四:自然排序算法――natsort()和natcasesort();
復(fù)制代碼 代碼如下:
<?php
$array = array("sort2","Sort5","sort1","sort4");
natsort($array);//從低到高排序
print_r($array);
echo "<br />";
natcasesort($array);
print_r($array);
?>

結(jié)果:
Array ( [1] => Sort5 [2] => sort1 [0] => sort2 [3] => sort4 )
Array ( [2] => sort1 [0] => sort2 [3] => sort4 [1] => Sort5 )
natsort()、natcasesort()對(duì)數(shù)組進(jìn)行自然排序,就是使用數(shù)字的正常排序算法。natcasesort會(huì)忽略大小寫。
實(shí)例五:對(duì)數(shù)組進(jìn)行鍵值排序ksort():
復(fù)制代碼 代碼如下:
<?php
$array = array(1=>"sort2",4=>"Sort5",2=>"sort1",3=>"sort4");
ksort($array);//從低到高排序
print_r($array);
?>

結(jié)果:
Array ( [1] => sort2 [2] => sort1 [3] => sort4 [4] => Sort5 )
注意:ksort()函數(shù)重新建立了索引。
八、數(shù)組的其他用法:
  cout($array) --------統(tǒng)計(jì)數(shù)組的單元個(gè)數(shù)
  array_diff($array1,$array2)----------統(tǒng)計(jì)數(shù)組之間的不同點(diǎn),返回第一個(gè)數(shù)組中有而第二個(gè)數(shù)組中沒有的。
  array_diff_assoc($array1,$array2)---------同array_diff(),只是它對(duì)鍵值也比較
  array_diff_key($array1,$array2)------------比較鍵值
  array_product($array)-----------返回?cái)?shù)組的所有數(shù)的乘積
  array_sum($array)--------------所有數(shù)值的和
  array_rand($array,$n)----------在$array數(shù)組中取出$n個(gè)數(shù)值,返回?cái)?shù)組
  array_intersect($array1,$array2)----------------取得兩個(gè)數(shù)組的交集
  array_intersect_assoc($array1,$array2)---------------在array_intersect 的基礎(chǔ)上進(jìn)行鍵值比較
  array_intersect_key($array1,$array2)-----------------比較兩個(gè)數(shù)組鍵值的交集
  總結(jié):
  數(shù)組的使用在php中至關(guān)重要,由于php沒有指針,所以數(shù)組承擔(dān)了很大的數(shù)據(jù)操作任務(wù)。學(xué)好數(shù)組,才能把php應(yīng)用的得心應(yīng)手,這里所列均是常用的php數(shù)組相關(guān)的函數(shù)及用法,歡迎一起學(xué)習(xí)!

php技術(shù)php 數(shù)組使用詳解 推薦,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 2021久久精品99精品久久 | 欧美中文小说在线观看 | 久久伊人精品热在75 | 深爱婷婷激情网 | 最新精品亚洲成a人在线观看 | 激情小视频在线播放免费 | 久久精品国产一区二区 | 色女人综合网 | 一区在线观看 | 精品国产一区二区三区在线观看 | 亚洲精品天堂在线观看 | 国产精品乱码免费一区二区 | 亚洲欧美另类国产综合 | 亚洲综合网国产福利精品一区 | 美女三级黄 | 加勒比一到三区 | caoporn人人| 热综合一本伊人久久精品 | 欧美成人免费午夜影视 | 亚洲影视在线观看 | 精品视频免费看 | 91福利刘玥国产在线观看 | www.色av.com| 欧美一级做一a做片性视频 欧美一级做一级做片性十三 | 亚洲免费h | 91久久青青草原线免费 | 在线免费一区二区 | 91精品国产自产在线观看 | 日本高清视频在线www色 | 成人国产激情福利久久精品 | 色哟哟在线免费观看 | 亚洲情趣 | 黄色激情视频在线观看 | 377日本大胆欧美人术艺术 | 曰本还a大片免费无播放器 曰本视频网络www色 | 中文字幕在线视频网站 | 久久91精品国产91久久户 | 蜜桃视频成人免费版在线 | 久久综合色区 | 玖玖玖视频在线观看视频6 玖玖免费 | 色婷婷一区二区三区四区成人网 |