第五章 php數(shù)組操作
更新時(shí)間:2011年12月30日 00:25:08 作者:
數(shù)組是一組有某種共同特性的元素,包括相似性和類型。每個(gè)元素由一個(gè)特殊的標(biāo)識符來區(qū)分,稱之為key,而每個(gè)key都有一個(gè)value
一.什么是數(shù)組
數(shù)組是一組有某種共同特性的元素,包括相似性和類型。
每個(gè)元素由一個(gè)特殊的標(biāo)識符來區(qū)分,稱之為key,而每個(gè)key都有一個(gè)value
1.創(chuàng)建數(shù)組的兩種方式:
1.1 用array()函數(shù)
<?php
$usernames = array ('Alerk', 'Mary', 'Lucy', 'Bob', 'Jack', 'John', 'Mark' );
foreach ( $usernames as $name )
{
echo $name . '<br/>';
}
?>
output
Alerk
Mary
Lucy
Bob
Jack
John
Mark
1.2 用range()函數(shù)
<?php
$numbers = range ( 0, 10 );
foreach ( $numbers as $num )
{
echo $num . '<br/>';
}
$letters = range ( 'a', 'z' );
foreach ( $letters as $letter )
{
echo $letter . '<br/>';
}
?>
output
0
1
2
3
4
5
6
7
8
9
10
a
c
d
e
f
g
h
i
j
k
l
m
o
q
r
t
u
v
w
x
y
z
2.循環(huán)訪問數(shù)組元素的兩種方式:
2.1 for循環(huán)
<?php
//range的第三個(gè)參數(shù)表示步長
$numbers = range(1,10,2);
for($i = 0;$i<count($numbers); $i ++)
{
echo $numbers[$i].'<br/>';
}
?>
output
1
3
5
7
9
2.2 foreach循環(huán)
<?php
$letters = range('a','h',2);
foreach($letters as $letter)
{
echo $letter.'<br/>';
}
?>
output
a
c
e
g
Foreach還可以用來輸出數(shù)組的下標(biāo)和對應(yīng)的值
<?php
$letters = range('a','g',2);
foreach($letters as $key => $value)
{
echo $key.'---'.$value.'<br/>';
}
?>
output
0---a
1---c
2---e
3---g
3.is_array()函數(shù),用于變量判斷是否為一個(gè)數(shù)組
<?php
$numbers = range(1,10,2);
if(is_array($numbers))
{
foreach($numbers as $num)
{
echo $num.'<br/>';
}
}
else
{
echo $numbers;
}
?>
4.print_r函數(shù),打印關(guān)于變量的易于理解的信息
<?php
$usernames = array ('Jackie', 'Mary', 'Lucy', 'Bob', 'Mark', 'John' );
print_r ( $usernames );
?>
output
Array ( [0] => Jackie [1] => Mary [2] => Lucy [3] => Bob [4] => Mark [5] => John )
源代碼里可以看到顯示為:
Array
(
[0] => Jackie
[1] => Mary
[2] => Lucy
[3] => Bob
[4] => Mark
[5] => John
)
二.自定義鍵數(shù)組
1.如果不想創(chuàng)建默認(rèn)下標(biāo)為零的數(shù)組,可以用如下方法,創(chuàng)建鍵為字符串的數(shù)組
<?php
//初始化數(shù)組
$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28);
//訪問數(shù)組各元素
echo $userages['Jack'].'<br/>';
echo $userages['Lucy'].'<br/>';
echo $userages['Mark'].'<br/>';
?>
2.往自定義鍵數(shù)組里追加元素
<?php
//初始化數(shù)組
$ages = array('Jack'=>23);
//追加元素
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'----'.$value.'<br/>';
}
?>
3.直接添加元素,無需創(chuàng)建數(shù)組。
<?php
//不創(chuàng)建數(shù)組直接添加
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'----'.$value.'<br/>';
}
?>
4.循環(huán)打印數(shù)組foreach的使用
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'=>'.$value.'<br/>';
}
?>
5. each() -- 返回?cái)?shù)組中當(dāng)前的鍵/值對并將數(shù)組指針向前移動一步
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
$a = each($ages);
print_r($a);
echo '<br/>';
$a = each($ages);
print_r($a);
echo '<br/>';
$a = each($ages);
print_r($a);
?>
用each()函數(shù)做循環(huán)打印
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!! $element = each($ages))
{
print_r($element);
echo '<br>';
}
?>
另一種打印方式
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!! $element = each($ages))
{
echo $element['key'].'=>'.$element['value'];
echo '<br>';
}
?>
6.list()函數(shù)的使用--把數(shù)組中的值賦給一些變量
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
list($name,$age)= each($ages);
echo $name.'=>'.$age;
?>
用list循環(huán)打印結(jié)果
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!!list($name,$age)= each($ages))
{
echo $name.'=>'.$age.'<br>';
}
?>
output
Jack=>23
Lucy=>25
Mark=>28
7.reset()函數(shù)的使用--將數(shù)組的內(nèi)部指針指向第一個(gè)單元
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
each($ages);
each($ages);
list($name,$age)= each($ages);
echo $name.'=>'.$age.'<br>';
//把數(shù)組重新設(shè)定到數(shù)組開始處
reset($ages);
list($name,$age)= each($ages);
echo $name.'=>'.$age.'<br>';
?>
Output
Mark=>28
Jack=>23
8. array_unique() -- 移除數(shù)組中重復(fù)的值
<?php
$nums = array(1,2,3,4,5,6,5,4,3,2,1,1,2,3,4,5,6);
//返回一個(gè)不包含重復(fù)值的數(shù)組
$result = array_unique($nums);
print_r($result);
?>
Output
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
9. array_flip ()-- 交換數(shù)組中的鍵和值
<?php
$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28);
$ages = array_flip($userages);
print_r($ages);
?>
output
Array ( [23] => Jack [25] => Lucy [28] => Mark )
三.?dāng)?shù)組里的數(shù)組
數(shù)組里不一定就是一個(gè)關(guān)鍵字和值的列表,數(shù)組里也可以放入數(shù)組
<?php
$produces = array(
array('apple',6,28.8),
array('pear',3,15.6),
array('banana',10,4.6)
);
echo $produces[0][0].'|'.$produces[0][1].'|'.$produces[0][2].'<br>';
echo $produces[1][0].'|'.$produces[1][1].'|'.$produces[1][2].'<br>';
echo $produces[2][0].'|'.$produces[2][1].'|'.$produces[2][2].'<br>';
?>
output
apple|6|28.8
pear|3|15.6
banana|10|4.6
用for循環(huán)打印數(shù)組中的數(shù)組
<?php
$produces = array (
array ('apple', 6, 28.8 ),
array ('pear', 3, 15.6 ),
array ('banana', 10, 4.6 )
);
for($i = 0; $i < count ( $produces ); $i ++)
{
for($j = 0; $j < count ( $produces [$i] ); $j ++)
{
echo '|' . $produces[$i][$j];
}
echo '<br>';
}
?>
output
|apple|6|28.8
|pear|3|15.6
|banana|10|4.6
二維數(shù)組
<?php
$produces = array (
array ('name' => 'apple', 'amount' => 6, 'price' => 28.8 ),
array ('name' => 'pear', 'amount' => 3, 'price' => 15.6 ),
array ('name' => 'banana', 'amount' => 10, 'price' => 4.6 )
);
while(!!List($key,$value)=each($produces))
{
while(!!list($key2,$value2)=each($value))
{
echo '|'.$key2.'=>'.$value2;
}
echo '<br>';
}
?>
output
|name=>apple|amount=>6|price=>28.8
|name=>pear|amount=>3|price=>15.6
|name=>banana|amount=>10|price=>4.6
用foreach來打印則更容易(推薦)
<?php
$produces = array (
array ('name' => 'apple', 'amount' => 6, 'price' => 28.8 ),
array ('name' => 'pear', 'amount' => 3, 'price' => 15.6 ),
array ('name' => 'banana', 'amount' => 10, 'price' => 4.6 )
);
foreach($produces as $key1 => $value1)
{
foreach($value1 as $key2 => $value2)
{
echo '|'.$key2.'=>'.$value2;
}
echo '<br>';
}
?>
output
|name=>apple|amount=>6|price=>28.8
|name=>pear|amount=>3|price=>15.6
|name=>banana|amount=>10|price=>4.6
四.?dāng)?shù)組的排序
1.Sort()函數(shù)對英文的排序
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('lemo','banana','apple','pear');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
sort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [0] => lemo [1] => banana [2] => apple [3] => pear )
排序后的數(shù)組:Array ( [0] => apple [1] => banana [2] => lemo [3] => pear )
2.Sort()函數(shù)對中文的排序
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('檸檬','香蕉','蘋果','梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
sort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
Output:
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
排序后的數(shù)組:Array ( [0] => 檸檬 [1] => 梨子 [2] => 蘋果 [3] => 香蕉 )
3. asort -- 對數(shù)組進(jìn)行排序并保持索引關(guān)系
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
asort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
排序后的數(shù)組:Array ( [a] => 檸檬 [d] => 梨子 [c] => 蘋果 [b] => 香蕉 )
4. ksort -- 對數(shù)組按照鍵名排序
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('b'=>'檸檬','a'=>'香蕉','d'=>'蘋果','c'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
ksort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [b] => 檸檬 [a] => 香蕉 [d] => 蘋果 [c] => 梨子 )
排序后的數(shù)組:Array ( [a] => 香蕉 [b] => 檸檬 [c] => 梨子 [d] => 蘋果 )
5. rsort -- 對數(shù)組逆向排序
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('檸檬','香蕉','蘋果','梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
rsort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
排序后的數(shù)組:Array ( [0] => 香蕉 [1] => 蘋果 [2] => 梨子 [3] => 檸檬 )
6. arsort -- 對數(shù)組進(jìn)行逆向排序并保持索引關(guān)系
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
arsort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
排序后的數(shù)組:Array ( [b] => 香蕉 [c] => 蘋果 [d] => 梨子 [a] => 檸檬 )
7. krsort -- 對數(shù)組按照鍵名逆向排序
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
krsort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
排序后的數(shù)組:Array ( [d] => 梨子 [c] => 蘋果 [b] => 香蕉 [a] => 檸檬 )
8. shuffle -- 將數(shù)組打亂
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
shuffle($fruits);
echo '打亂后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
打亂后的數(shù)組:Array ( [0] => 香蕉 [1] => 蘋果 [2] => 檸檬 [3] => 梨子 )
9. array_reverse -- 返回一個(gè)單元順序相反的數(shù)組
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
$fruits = array_reverse($fruits);
echo '反轉(zhuǎn)后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
反轉(zhuǎn)后的數(shù)組:Array ( [d] => 梨子 [c] => 蘋果 [b] => 香蕉 [a] => 檸檬 )
10. array_unshift -- 在數(shù)組開頭插入一個(gè)或多個(gè)單元
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
array_unshift($fruits,'杮子');
echo '插入后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
插入后的數(shù)組:Array ( [0] => 杮子 [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
11. array_shift -- 將數(shù)組開頭的單元移出數(shù)組
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
array_shift($fruits);
echo '移出后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
移出后的數(shù)組:Array ( [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
12. array_rand -- 從數(shù)組中隨機(jī)取出一個(gè)或多個(gè)單元
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array ('檸檬', '香蕉', '蘋果', '梨子' );
echo '原始的數(shù)組:';
print_r ( $fruits );
echo '<br/>';
$newArr_key = array_rand ( $fruits, 2 );
echo '隨機(jī)后的數(shù)組:';
echo $fruits [$newArr_key [0]].' ';
echo $fruits [$newArr_key [1]];
?>
output
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
隨機(jī)后的數(shù)組:梨子 蘋果
13. array_pop -- 將數(shù)組最后一個(gè)單元彈出(出棧)
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array ('檸檬', '香蕉', '蘋果', '梨子' );
echo '原始的數(shù)組:';
print_r ( $fruits );
echo '<br/>';
array_pop ( $fruits );
echo '彈出后的數(shù)組:';
print_r ( $fruits );
?>
Output:
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
彈出后的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 )
14. array_push -- 將一個(gè)或多個(gè)單元壓入數(shù)組的末尾(入棧)
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array ('檸檬', '香蕉', '蘋果', '梨子' );
echo '原始的數(shù)組:';
print_r ( $fruits );
echo '<br/>';
array_push ( $fruits,'杮子');
echo '彈出后的數(shù)組:';
print_r ( $fruits );
?>
Output:
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
彈出后的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 [4] => 杮子 )
五.?dāng)?shù)組的指針的操作
each -- 返回?cái)?shù)組中當(dāng)前的鍵/值對并將數(shù)組指針向前移動一步
current -- 返回?cái)?shù)組中的當(dāng)前單元
reset -- 將數(shù)組的內(nèi)部指針指向第一個(gè)單元
end -- 將數(shù)組的內(nèi)部指針指向最后一個(gè)單元
next -- 將數(shù)組中的內(nèi)部指針向前移動一位
pos -- current() 的別名
prev -- 將數(shù)組的內(nèi)部指針倒回一位
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array ('檸檬', '香蕉', '蘋果', '梨子' );
print_r ( $fruits );
echo '<br/>';
echo 'each() : ';
print_r ( each ( $fruits ) );
echo '<br/>';
echo 'current() : ';
echo (current ( $fruits ));
echo '<br/>';
echo 'next() : ';
echo (next ( $fruits ));
echo '<br/>';
echo 'end() : ';
echo (end ( $fruits ));
echo '<br/>';
echo 'prev() : ';
echo (prev ( $fruits ));
echo '<br/>';
echo 'pos() : ';
echo (pos ( $fruits ));
echo '<br/>';
?>
Output:
Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
each() : Array ( [1] => 檸檬 [value] => 檸檬 [0] => 0 [key] => 0 )
current() : 香蕉
next() : 蘋果
end() : 梨子
prev() : 蘋果
pos() : 蘋果
六.統(tǒng)計(jì)數(shù)組個(gè)數(shù)
count -- 計(jì)算數(shù)組中的單元數(shù)目或?qū)ο笾械膶傩詡€(gè)數(shù)
sizeof -- count() 的別名
array_count_values -- 統(tǒng)計(jì)數(shù)組中所有的值出現(xiàn)的次數(shù)
<?php
$nums = array (1, 3, 5, 1, 3, 4, 5, 65, 4, 2, 2, 1, 4, 4, 1, 1, 4, 1, 5, 4, 5, 4 );
echo count ( $nums );
echo '<br>';
echo sizeof ( $nums );
echo '<br>';
$arrayCount = array_count_values ( $nums );
print_r ( $arrayCount );
?>
output
22
22
Array ( [1] => 6 [3] => 2 [5] => 4 [4] => 7 [65] => 1 [2] => 2 )
七.將數(shù)組轉(zhuǎn)換成標(biāo)量變量:extract()
把數(shù)組中的每個(gè)元素轉(zhuǎn)換成變量,變量名是數(shù)組元素的key,變量值為數(shù)組元素的value.
<?php
$fruits = array('a'=>'apple','b'=>'banana','o'=>'orange');
extract($fruits);
echo $a.'<br>';
echo $b.'<br>';
echo $o.'<br>';
?>
output
apple
banana
orange
數(shù)組是一組有某種共同特性的元素,包括相似性和類型。
每個(gè)元素由一個(gè)特殊的標(biāo)識符來區(qū)分,稱之為key,而每個(gè)key都有一個(gè)value
1.創(chuàng)建數(shù)組的兩種方式:
1.1 用array()函數(shù)
復(fù)制代碼 代碼如下:
<?php
$usernames = array ('Alerk', 'Mary', 'Lucy', 'Bob', 'Jack', 'John', 'Mark' );
foreach ( $usernames as $name )
{
echo $name . '<br/>';
}
?>
output
Alerk
Mary
Lucy
Bob
Jack
John
Mark
1.2 用range()函數(shù)
復(fù)制代碼 代碼如下:
<?php
$numbers = range ( 0, 10 );
foreach ( $numbers as $num )
{
echo $num . '<br/>';
}
$letters = range ( 'a', 'z' );
foreach ( $letters as $letter )
{
echo $letter . '<br/>';
}
?>
output
0
1
2
3
4
5
6
7
8
9
10
a
c
d
e
f
g
h
i
j
k
l
m
o
q
r
t
u
v
w
x
y
z
2.循環(huán)訪問數(shù)組元素的兩種方式:
2.1 for循環(huán)
復(fù)制代碼 代碼如下:
<?php
//range的第三個(gè)參數(shù)表示步長
$numbers = range(1,10,2);
for($i = 0;$i<count($numbers); $i ++)
{
echo $numbers[$i].'<br/>';
}
?>
output
1
3
5
7
9
2.2 foreach循環(huán)
復(fù)制代碼 代碼如下:
<?php
$letters = range('a','h',2);
foreach($letters as $letter)
{
echo $letter.'<br/>';
}
?>
output
a
c
e
g
Foreach還可以用來輸出數(shù)組的下標(biāo)和對應(yīng)的值
復(fù)制代碼 代碼如下:
<?php
$letters = range('a','g',2);
foreach($letters as $key => $value)
{
echo $key.'---'.$value.'<br/>';
}
?>
output
0---a
1---c
2---e
3---g
3.is_array()函數(shù),用于變量判斷是否為一個(gè)數(shù)組
復(fù)制代碼 代碼如下:
<?php
$numbers = range(1,10,2);
if(is_array($numbers))
{
foreach($numbers as $num)
{
echo $num.'<br/>';
}
}
else
{
echo $numbers;
}
?>
4.print_r函數(shù),打印關(guān)于變量的易于理解的信息
復(fù)制代碼 代碼如下:
<?php
$usernames = array ('Jackie', 'Mary', 'Lucy', 'Bob', 'Mark', 'John' );
print_r ( $usernames );
?>
output
Array ( [0] => Jackie [1] => Mary [2] => Lucy [3] => Bob [4] => Mark [5] => John )
源代碼里可以看到顯示為:
Array
(
[0] => Jackie
[1] => Mary
[2] => Lucy
[3] => Bob
[4] => Mark
[5] => John
)
二.自定義鍵數(shù)組
1.如果不想創(chuàng)建默認(rèn)下標(biāo)為零的數(shù)組,可以用如下方法,創(chuàng)建鍵為字符串的數(shù)組
復(fù)制代碼 代碼如下:
<?php
//初始化數(shù)組
$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28);
//訪問數(shù)組各元素
echo $userages['Jack'].'<br/>';
echo $userages['Lucy'].'<br/>';
echo $userages['Mark'].'<br/>';
?>
2.往自定義鍵數(shù)組里追加元素
復(fù)制代碼 代碼如下:
<?php
//初始化數(shù)組
$ages = array('Jack'=>23);
//追加元素
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'----'.$value.'<br/>';
}
?>
3.直接添加元素,無需創(chuàng)建數(shù)組。
復(fù)制代碼 代碼如下:
<?php
//不創(chuàng)建數(shù)組直接添加
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'----'.$value.'<br/>';
}
?>
4.循環(huán)打印數(shù)組foreach的使用
復(fù)制代碼 代碼如下:
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'=>'.$value.'<br/>';
}
?>
5. each() -- 返回?cái)?shù)組中當(dāng)前的鍵/值對并將數(shù)組指針向前移動一步
復(fù)制代碼 代碼如下:
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
$a = each($ages);
print_r($a);
echo '<br/>';
$a = each($ages);
print_r($a);
echo '<br/>';
$a = each($ages);
print_r($a);
?>
用each()函數(shù)做循環(huán)打印
復(fù)制代碼 代碼如下:
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!! $element = each($ages))
{
print_r($element);
echo '<br>';
}
?>
另一種打印方式
復(fù)制代碼 代碼如下:
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!! $element = each($ages))
{
echo $element['key'].'=>'.$element['value'];
echo '<br>';
}
?>
6.list()函數(shù)的使用--把數(shù)組中的值賦給一些變量
復(fù)制代碼 代碼如下:
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
list($name,$age)= each($ages);
echo $name.'=>'.$age;
?>
用list循環(huán)打印結(jié)果
復(fù)制代碼 代碼如下:
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!!list($name,$age)= each($ages))
{
echo $name.'=>'.$age.'<br>';
}
?>
output
Jack=>23
Lucy=>25
Mark=>28
7.reset()函數(shù)的使用--將數(shù)組的內(nèi)部指針指向第一個(gè)單元
復(fù)制代碼 代碼如下:
<?php
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
each($ages);
each($ages);
list($name,$age)= each($ages);
echo $name.'=>'.$age.'<br>';
//把數(shù)組重新設(shè)定到數(shù)組開始處
reset($ages);
list($name,$age)= each($ages);
echo $name.'=>'.$age.'<br>';
?>
Output
Mark=>28
Jack=>23
8. array_unique() -- 移除數(shù)組中重復(fù)的值
復(fù)制代碼 代碼如下:
<?php
$nums = array(1,2,3,4,5,6,5,4,3,2,1,1,2,3,4,5,6);
//返回一個(gè)不包含重復(fù)值的數(shù)組
$result = array_unique($nums);
print_r($result);
?>
Output
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
9. array_flip ()-- 交換數(shù)組中的鍵和值
<?php
$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28);
$ages = array_flip($userages);
print_r($ages);
?>
output
Array ( [23] => Jack [25] => Lucy [28] => Mark )
三.?dāng)?shù)組里的數(shù)組
數(shù)組里不一定就是一個(gè)關(guān)鍵字和值的列表,數(shù)組里也可以放入數(shù)組
復(fù)制代碼 代碼如下:
<?php
$produces = array(
array('apple',6,28.8),
array('pear',3,15.6),
array('banana',10,4.6)
);
echo $produces[0][0].'|'.$produces[0][1].'|'.$produces[0][2].'<br>';
echo $produces[1][0].'|'.$produces[1][1].'|'.$produces[1][2].'<br>';
echo $produces[2][0].'|'.$produces[2][1].'|'.$produces[2][2].'<br>';
?>
output
apple|6|28.8
pear|3|15.6
banana|10|4.6
用for循環(huán)打印數(shù)組中的數(shù)組
復(fù)制代碼 代碼如下:
<?php
$produces = array (
array ('apple', 6, 28.8 ),
array ('pear', 3, 15.6 ),
array ('banana', 10, 4.6 )
);
for($i = 0; $i < count ( $produces ); $i ++)
{
for($j = 0; $j < count ( $produces [$i] ); $j ++)
{
echo '|' . $produces[$i][$j];
}
echo '<br>';
}
?>
output
|apple|6|28.8
|pear|3|15.6
|banana|10|4.6
二維數(shù)組
復(fù)制代碼 代碼如下:
<?php
$produces = array (
array ('name' => 'apple', 'amount' => 6, 'price' => 28.8 ),
array ('name' => 'pear', 'amount' => 3, 'price' => 15.6 ),
array ('name' => 'banana', 'amount' => 10, 'price' => 4.6 )
);
while(!!List($key,$value)=each($produces))
{
while(!!list($key2,$value2)=each($value))
{
echo '|'.$key2.'=>'.$value2;
}
echo '<br>';
}
?>
output
|name=>apple|amount=>6|price=>28.8
|name=>pear|amount=>3|price=>15.6
|name=>banana|amount=>10|price=>4.6
用foreach來打印則更容易(推薦)
復(fù)制代碼 代碼如下:
<?php
$produces = array (
array ('name' => 'apple', 'amount' => 6, 'price' => 28.8 ),
array ('name' => 'pear', 'amount' => 3, 'price' => 15.6 ),
array ('name' => 'banana', 'amount' => 10, 'price' => 4.6 )
);
foreach($produces as $key1 => $value1)
{
foreach($value1 as $key2 => $value2)
{
echo '|'.$key2.'=>'.$value2;
}
echo '<br>';
}
?>
output
|name=>apple|amount=>6|price=>28.8
|name=>pear|amount=>3|price=>15.6
|name=>banana|amount=>10|price=>4.6
四.?dāng)?shù)組的排序
1.Sort()函數(shù)對英文的排序
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('lemo','banana','apple','pear');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
sort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [0] => lemo [1] => banana [2] => apple [3] => pear )
排序后的數(shù)組:Array ( [0] => apple [1] => banana [2] => lemo [3] => pear )
2.Sort()函數(shù)對中文的排序
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('檸檬','香蕉','蘋果','梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
sort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
Output:
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
排序后的數(shù)組:Array ( [0] => 檸檬 [1] => 梨子 [2] => 蘋果 [3] => 香蕉 )
3. asort -- 對數(shù)組進(jìn)行排序并保持索引關(guān)系
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
asort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
排序后的數(shù)組:Array ( [a] => 檸檬 [d] => 梨子 [c] => 蘋果 [b] => 香蕉 )
4. ksort -- 對數(shù)組按照鍵名排序
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('b'=>'檸檬','a'=>'香蕉','d'=>'蘋果','c'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
ksort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [b] => 檸檬 [a] => 香蕉 [d] => 蘋果 [c] => 梨子 )
排序后的數(shù)組:Array ( [a] => 香蕉 [b] => 檸檬 [c] => 梨子 [d] => 蘋果 )
5. rsort -- 對數(shù)組逆向排序
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('檸檬','香蕉','蘋果','梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
rsort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
排序后的數(shù)組:Array ( [0] => 香蕉 [1] => 蘋果 [2] => 梨子 [3] => 檸檬 )
6. arsort -- 對數(shù)組進(jìn)行逆向排序并保持索引關(guān)系
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
arsort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
排序后的數(shù)組:Array ( [b] => 香蕉 [c] => 蘋果 [d] => 梨子 [a] => 檸檬 )
7. krsort -- 對數(shù)組按照鍵名逆向排序
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
krsort($fruits);
echo '排序后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
排序后的數(shù)組:Array ( [d] => 梨子 [c] => 蘋果 [b] => 香蕉 [a] => 檸檬 )
8. shuffle -- 將數(shù)組打亂
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
shuffle($fruits);
echo '打亂后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
打亂后的數(shù)組:Array ( [0] => 香蕉 [1] => 蘋果 [2] => 檸檬 [3] => 梨子 )
9. array_reverse -- 返回一個(gè)單元順序相反的數(shù)組
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
$fruits = array_reverse($fruits);
echo '反轉(zhuǎn)后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
反轉(zhuǎn)后的數(shù)組:Array ( [d] => 梨子 [c] => 蘋果 [b] => 香蕉 [a] => 檸檬 )
10. array_unshift -- 在數(shù)組開頭插入一個(gè)或多個(gè)單元
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
array_unshift($fruits,'杮子');
echo '插入后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
插入后的數(shù)組:Array ( [0] => 杮子 [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
11. array_shift -- 將數(shù)組開頭的單元移出數(shù)組
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array('a'=>'檸檬','b'=>'香蕉','c'=>'蘋果','d'=>'梨子');
echo '原始的數(shù)組:';
print_r($fruits);
echo '<br/>';
array_shift($fruits);
echo '移出后的數(shù)組:';
print_r($fruits);
?>
output
原始的數(shù)組:Array ( [a] => 檸檬 [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
移出后的數(shù)組:Array ( [b] => 香蕉 [c] => 蘋果 [d] => 梨子 )
12. array_rand -- 從數(shù)組中隨機(jī)取出一個(gè)或多個(gè)單元
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array ('檸檬', '香蕉', '蘋果', '梨子' );
echo '原始的數(shù)組:';
print_r ( $fruits );
echo '<br/>';
$newArr_key = array_rand ( $fruits, 2 );
echo '隨機(jī)后的數(shù)組:';
echo $fruits [$newArr_key [0]].' ';
echo $fruits [$newArr_key [1]];
?>
output
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
隨機(jī)后的數(shù)組:梨子 蘋果
13. array_pop -- 將數(shù)組最后一個(gè)單元彈出(出棧)
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array ('檸檬', '香蕉', '蘋果', '梨子' );
echo '原始的數(shù)組:';
print_r ( $fruits );
echo '<br/>';
array_pop ( $fruits );
echo '彈出后的數(shù)組:';
print_r ( $fruits );
?>
Output:
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
彈出后的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 )
14. array_push -- 將一個(gè)或多個(gè)單元壓入數(shù)組的末尾(入棧)
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$fruits = array ('檸檬', '香蕉', '蘋果', '梨子' );
echo '原始的數(shù)組:';
print_r ( $fruits );
echo '<br/>';
array_push ( $fruits,'杮子');
echo '彈出后的數(shù)組:';
print_r ( $fruits );
?>
Output:
原始的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
彈出后的數(shù)組:Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 [4] => 杮子 )
五.?dāng)?shù)組的指針的操作
each -- 返回?cái)?shù)組中當(dāng)前的鍵/值對并將數(shù)組指針向前移動一步
current -- 返回?cái)?shù)組中的當(dāng)前單元
reset -- 將數(shù)組的內(nèi)部指針指向第一個(gè)單元
end -- 將數(shù)組的內(nèi)部指針指向最后一個(gè)單元
next -- 將數(shù)組中的內(nèi)部指針向前移動一位
pos -- current() 的別名
prev -- 將數(shù)組的內(nèi)部指針倒回一位
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
復(fù)制代碼 代碼如下:
<?php
$fruits = array ('檸檬', '香蕉', '蘋果', '梨子' );
print_r ( $fruits );
echo '<br/>';
echo 'each() : ';
print_r ( each ( $fruits ) );
echo '<br/>';
echo 'current() : ';
echo (current ( $fruits ));
echo '<br/>';
echo 'next() : ';
echo (next ( $fruits ));
echo '<br/>';
echo 'end() : ';
echo (end ( $fruits ));
echo '<br/>';
echo 'prev() : ';
echo (prev ( $fruits ));
echo '<br/>';
echo 'pos() : ';
echo (pos ( $fruits ));
echo '<br/>';
?>
Output:
Array ( [0] => 檸檬 [1] => 香蕉 [2] => 蘋果 [3] => 梨子 )
each() : Array ( [1] => 檸檬 [value] => 檸檬 [0] => 0 [key] => 0 )
current() : 香蕉
next() : 蘋果
end() : 梨子
prev() : 蘋果
pos() : 蘋果
六.統(tǒng)計(jì)數(shù)組個(gè)數(shù)
count -- 計(jì)算數(shù)組中的單元數(shù)目或?qū)ο笾械膶傩詡€(gè)數(shù)
sizeof -- count() 的別名
array_count_values -- 統(tǒng)計(jì)數(shù)組中所有的值出現(xiàn)的次數(shù)
復(fù)制代碼 代碼如下:
<?php
$nums = array (1, 3, 5, 1, 3, 4, 5, 65, 4, 2, 2, 1, 4, 4, 1, 1, 4, 1, 5, 4, 5, 4 );
echo count ( $nums );
echo '<br>';
echo sizeof ( $nums );
echo '<br>';
$arrayCount = array_count_values ( $nums );
print_r ( $arrayCount );
?>
output
22
22
Array ( [1] => 6 [3] => 2 [5] => 4 [4] => 7 [65] => 1 [2] => 2 )
七.將數(shù)組轉(zhuǎn)換成標(biāo)量變量:extract()
把數(shù)組中的每個(gè)元素轉(zhuǎn)換成變量,變量名是數(shù)組元素的key,變量值為數(shù)組元素的value.
復(fù)制代碼 代碼如下:
<?php
$fruits = array('a'=>'apple','b'=>'banana','o'=>'orange');
extract($fruits);
echo $a.'<br>';
echo $b.'<br>';
echo $o.'<br>';
?>
output
apple
banana
orange
相關(guān)文章
利用static實(shí)現(xiàn)表格的顏色隔行顯示
利用static實(shí)現(xiàn)表格的顏色隔行顯示...2006-10-10PHP輸出控制功能在簡繁體轉(zhuǎn)換中的應(yīng)用
PHP輸出控制功能在簡繁體轉(zhuǎn)換中的應(yīng)用...2006-10-10PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特性
PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特性...2006-10-10