介紹幾個 php4 中非常有用的數組函數(轉載)關聯陣列等同於PERL裡的雜湊數組。以前我一直以為PHP裡沒...
來源:互聯網
上載者:User
perl|函數|數組 介紹幾個 php4 中非常有用的"數組"函數
1 void extract (array var_array [, int extract_type ][, string prefix]])
把一個關聯陣列展開為變數名和變數的值,如果有衝突則由後面的參數指定處理方法!
如:
<php?
/* 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
)