手册上说:
array_values -- 返回数组中所有的值
array array_values ( array input )
array_values() 返回 input 数组中所有的值并给其建立数字索引。
<?
php
$array = array ( " size " => " XL " , " color " => " gold " );
print_r ( array_values ( $array ));
?>
$array = array ( " size " => " XL " , " color " => " gold " );
print_r ( array_values ( $array ));
?>
Array
(
[ 0 ] => XL
[ 1 ] => gold
)
(
[ 0 ] => XL
[ 1 ] => gold
)
这就决定该函数还有另一个功能,就是对数组进行重新索引(数字索引)
如下面在孙鹏程所著完全手册里的一个例子
<?
php
$array = array ( 0 => " a " , 1 => " b " , 2 => " c " , 3 => " d " , 4 => " e " ); // 建立一个新数组
print_r ( $array );
$array [ 2 ] = " m " ; // 更新键为2的数组元素的值为m
print_r ( $array );
$array [ ' x ' ] = " o " ; // 添加一个键为x
print_r ( $array );
foreach ( $array as $key => $ar ){ // 删除数组中所有的键,但是保持数组本身的结构
unset ( $array [ $key ]);
}
print_r ( $array );
$array [] = " y " ; // 新加一个元素
print_r ( $array );
$array = array_values ( $array ); // 对数组进行重新数字索引
$array [] = " end " ; // 再新加一个元素一会儿查看元素的键
print_r ( $array );
?>
$array = array ( 0 => " a " , 1 => " b " , 2 => " c " , 3 => " d " , 4 => " e " ); // 建立一个新数组
print_r ( $array );
$array [ 2 ] = " m " ; // 更新键为2的数组元素的值为m
print_r ( $array );
$array [ ' x ' ] = " o " ; // 添加一个键为x
print_r ( $array );
foreach ( $array as $key => $ar ){ // 删除数组中所有的键,但是保持数组本身的结构
unset ( $array [ $key ]);
}
print_r ( $array );
$array [] = " y " ; // 新加一个元素
print_r ( $array );
$array = array_values ( $array ); // 对数组进行重新数字索引
$array [] = " end " ; // 再新加一个元素一会儿查看元素的键
print_r ( $array );
?>
输出结果为:
Array
(
[ 0 ] => a
[ 1 ] => b
[ 2 ] => c
[ 3 ] => d
[ 4 ] => e
)
Array
(
[ 0 ] => a
[ 1 ] => b
[ 2 ] => m
[ 3 ] => d
[ 4 ] => e
)
Array
(
[ 0 ] => a
[ 1 ] => b
[ 2 ] => m
[ 3 ] => d
[ 4 ] => e
[x] => o
)
Array
(
)
Array
(
[ 5 ] => y
)
Array
(
[ 0 ] => y
[ 1 ] => end
)
(
[ 0 ] => a
[ 1 ] => b
[ 2 ] => c
[ 3 ] => d
[ 4 ] => e
)
Array
(
[ 0 ] => a
[ 1 ] => b
[ 2 ] => m
[ 3 ] => d
[ 4 ] => e
)
Array
(
[ 0 ] => a
[ 1 ] => b
[ 2 ] => m
[ 3 ] => d
[ 4 ] => e
[x] => o
)
Array
(
)
Array
(
[ 5 ] => y
)
Array
(
[ 0 ] => y
[ 1 ] => end
)