介绍几个 php4 中非常有用的数组函数(转载)关联数组等同于PERL里的哈希数组。以前我一直以为PHP里没...
perl|函数|数组
介绍几个 php4 中非常有用的"数组"函数
1 void extract (array var_array [, int extract_type ][, string prefix]])
把一个关联数组展开为变量名和变量的值,如果有冲突则由后面的参数指定处理方法!
如:
/* Suppose that $var_array is an array returned from
wddx_deserialize */
$size = "large";
$var_array = array ("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract ($var_array, EXTR_PREFIX_SAME, "wddx");
print "$color, $size, $shape, $wddx_sizen";
?>
2 array compact (mixed varname [, mixed ...])
和上面的函数相反,把变量名和变量的值保存到关联数组里面!
如:
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location_vars = array ("city", "state");
$result = compact ("event", "nothing_here", $location_vars);
$result 结果为 array ("event" => "SIGGRAPH", "city" => "San Francisco", "state" => "CA").
3 bool in_array (mixed needle, array haystack)
判断数组中是否有这个值
4 void natsort (array array)
以自然数的方法排序数组,这时 12 将排在2的后面
$array1 = $array2 = array ("img12.png","img10.png","img2.png","img1.png");
sort($array1);
echo "标准排序n";
print_r($array1);
natsort($array2);
echo "n自然排序n";
print_r($array2);
代码输出为:
标准排序
Array
(
[0] => img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)
自然排序
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)