# 第五章 数组

1.创建数组的两种方式：

1.1 用array()函数

``````<?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()函数

``````<?php

\$numbers = range ( 0, 10 );

foreach ( \$numbers as \$num )

{

echo \$num . '<br/>';

}

\$letters= range ( 'a', 'z' );

foreach ( \$lettersas \$letter )

{

echo \$letter . '<br/>';

}

?>
``````

output

``````0
1
2
3
4
5
6
7
8
9
10
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
``````

2.循环访问数组元素的两种方式:

2.1 for循环

``````<?php

//range的第三个参数表示步长

\$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循环

``````
<?php

\$letters = range('a','h',2);

foreach(\$letters as \$letter)

{

echo \$letter.'<br/>';

}

?>
``````

output

``````
a
c
e
g
``````

Foreach还可以用来输出数组的下标和对应的值

``````
<?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()函数，用于变量判断是否为一个数组

``````
<?php

\$numbers = range(1,10,2);

if(is_array(\$numbers))

{

foreach(\$numbers as \$num)

{

echo \$num.'<br/>';

}

}

else

{

echo \$numbers;

}

?>
``````

4.print_r函数，打印关于变量的易于理解的信息

``````
<?php

\$usernames = array ('Jackie', 'Mary', 'Lucy', 'Bob', 'Mark', 'John' );

?>
``````

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

)
``````

1.如果不想创建默认下标为零的数组，可以用如下方法，创建键为字符串的数组

``````
<?php

//初始化数组

\$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28);

//访问数组各元素

echo \$userages['Jack'].'<br/>';

echo \$userages['Lucy'].'<br/>';

echo \$userages['Mark'].'<br/>';

?>
``````

2.往自定义键数组里追加元素

``````
<?php

//初始化数组

\$ages = array('Jack'=>23);

//追加元素

\$ages['Lucy']=25;

\$ages['Mark']=28;

foreach(\$ages as \$key => \$value)

{

echo \$key.'----'.\$value.'<br/>';

}

?>
``````

3.直接添加元素，无需创建数组。

``````
<?php

//不创建数组直接添加

\$ages['Jack']=23;

\$ages['Lucy']=25;

\$ages['Mark']=28;

foreach(\$ages as \$key => \$value)

{

echo \$key.'----'.\$value.'<br/>';

}

?>
``````

4.循环打印数组foreach的使用

``````
<?php

\$ages['Jack']=23;

\$ages['Lucy']=25;

\$ages['Mark']=28;

foreach(\$ages as \$key => \$value)

{

echo \$key.'=>'.\$value.'<br/>';

}

?>
``````

5. each() -- 返回数组中当前的键／值对并将数组指针向前移动一步

``````<?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);

?>
``````

``````
<?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()函数的使用--把数组中的值赋给一些变量

``````<?php

\$ages['Jack']=23;

\$ages['Lucy']=25;

\$ages['Mark']=28;

list(\$name,\$age)= each(\$ages);

echo \$name.'=>'.\$age;

?>
``````

``````
<?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()函数的使用--将数组的内部指针指向第一个单元

``````<?php

\$ages['Jack']=23;

\$ages['Lucy']=25;

\$ages['Mark']=28;

each(\$ages);

each(\$ages);

list(\$name,\$age)= each(\$ages);

echo \$name.'=>'.\$age.'<br>';

//把数组重新设定到数组开始处

reset(\$ages);

list(\$name,\$age)= each(\$ages);

echo \$name.'=>'.\$age.'<br>';

?>
``````

Output

``````
Mark=>28
Jack=>23
``````

8. array_unique() -- 移除数组中重复的值

``````<?php

\$nums = array(1,2,3,4,5,6,5,4,3,2,1,1,2,3,4,5,6);

//返回一个不包含重复值的数组

\$result = array_unique(\$nums);

print_r(\$result);

?>
``````

Output

``````
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
``````

9. array_flip ()-- 交换数组中的键和值

``````<?php

\$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28);

\$ages = array_flip(\$userages);

print_r(\$ages);

?>
``````

output

``````
Array ( [23] => Jack [25] => Lucy [28] => Mark )
``````

``````
<?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
``````

``````<?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
``````

``````<?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
``````

``````<?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
``````

1.Sort()函数对英文的排序

``````<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('lemo','banana','apple','pear');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

sort(\$fruits);

echo '排序后的数组：';

print_r(\$fruits);

?>
``````

output

``````

``````

2.Sort()函数对中文的排序

``````
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('柠檬','香蕉','苹果','梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

sort(\$fruits);

echo '排序后的数组：';

print_r(\$fruits);

?>
``````

Output:

``````原始的数组：Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 [3] => 梨子 )

``````

3. asort -- 对数组进行排序并保持索引关系

``````
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

asort(\$fruits);

echo '排序后的数组：';

print_r(\$fruits);

?>
``````

output

``````原始的数组：Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )

``````

4. ksort -- 对数组按照键名排序

``````
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('b'=>'柠檬','a'=>'香蕉','d'=>'苹果','c'=>'梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

ksort(\$fruits);

echo '排序后的数组：';

print_r(\$fruits);

?>
``````

output

``````

``````

5. rsort -- 对数组逆向排序

``````<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('柠檬','香蕉','苹果','梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

rsort(\$fruits);

echo '排序后的数组：';

print_r(\$fruits);

?>
``````

output

``````

``````

6. arsort -- 对数组进行逆向排序并保持索引关系

``````<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

arsort(\$fruits);

echo '排序后的数组：';

print_r(\$fruits);

?>
``````

output

``````原始的数组：Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )

``````

7. krsort -- 对数组按照键名逆向排序

``````<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

krsort(\$fruits);

echo '排序后的数组：';

print_r(\$fruits);

?>
``````

output

``````原始的数组：Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )

``````

8. shuffle -- 将数组打乱

``````<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits= array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

shuffle(\$fruits);

echo '打乱后的数组：';

print_r(\$fruits);

?>
``````

output

``````原始的数组：Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )

``````

9. array_reverse -- 返回一个单元顺序相反的数组

``````
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

\$fruits = array_reverse(\$fruits);

echo '反转后的数组：';

print_r(\$fruits);

?>
``````

output

``````

``````

10. array_unshift -- 在数组开头插入一个或多个单元

``````
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

array_unshift(\$fruits,'杮子');

echo '插入后的数组：';

print_r(\$fruits);

?>
``````

output

``````

``````

11. array_shift -- 将数组开头的单元移出数组

``````<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');

echo '原始的数组：';

print_r(\$fruits);

echo '<br/>';

array_shift(\$fruits);

echo '移出后的数组：';

print_r(\$fruits);

?>
``````

output

``````

``````

12. array_rand -- 从数组中随机取出一个或多个单元

``````
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array ('柠檬', '香蕉', '苹果', '梨子' );

echo '原始的数组：';

print_r ( \$fruits );

echo '<br/>';

\$newArr_key = array_rand ( \$fruits, 2 );

echo '随机后的数组：';

echo \$fruits [\$newArr_key [0]].'&nbsp;';

echo \$fruits [\$newArr_key [1]];

?>
``````

output

``````

``````

13. array_pop -- 将数组最后一个单元弹出（出栈）

``````
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array ('柠檬', '香蕉', '苹果', '梨子' );

echo '原始的数组：';

print_r ( \$fruits );

echo '<br/>';

array_pop ( \$fruits );

echo '弹出后的数组：';

print_r ( \$fruits );

?>
``````

Output:

``````

``````

14. array_push -- 将一个或多个单元压入数组的末尾（入栈）

``````
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php

\$fruits = array ('柠檬', '香蕉', '苹果', '梨子' );

echo '原始的数组：';

print_r ( \$fruits );

echo '<br/>';

array_push ( \$fruits,'杮子');

echo '弹出后的数组：';

print_r ( \$fruits );

?>
``````

Output:

``````

``````

each -- 返回数组中当前的键／值对并将数组指针向前移动一步

current -- 返回数组中的当前单元

reset -- 将数组的内部指针指向第一个单元

end -- 将数组的内部指针指向最后一个单元

next -- 将数组中的内部指针向前移动一位

pos -- current() 的别名

prev -- 将数组的内部指针倒回一位

``````<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() : 苹果
``````

count -- 计算数组中的单元数目或对象中的属性个数

sizeof -- count() 的别名

array_count_values -- 统计数组中所有的值出现的次数

``````<?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 )
``````

``````
<?php

\$fruits = array('a'=>'apple','b'=>'banana','o'=>'orange');

extract(\$fruits);

echo \$a.'<br>';

echo \$b.'<br>';

echo \$o.'<br>';

?>
``````

output

``````
apple
banana
orange
``````