介紹幾個(gè) php4 中非常有用的"數(shù)組"函數(shù)
1 void extract (array var_array [, int extract_type ][, string prefix]]) 把一個(gè)關(guān)聯(lián)數(shù)組展開為變量名和變量的值,如果有沖突則由后面的參數(shù)指定處理方法! 如:
<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 ...]) 和上面的函數(shù)相反,把變量名和變量的值保存到關(guān)聯(lián)數(shù)組里面! 如: $city = "San Francisco"; $state = "CA"; $event = "SIGGRAPH";
$location_vars = array ("city", "state");
$result = compact ("event", "nothing_here", $location_vars);
$result 結(jié)果為 array ("event" => "SIGGRAPH", "city" => "San Francisco", "state" => "CA").
3 bool in_array (mixed needle, array haystack) 判斷數(shù)組中是否有這個(gè)值
4 void natsort (array array) 以自然數(shù)的方法排序數(shù)組,這時(shí) 12 將排在2的后面 $array1 = $array2 = array ("img12.png","img10.png","img2.png","img1.png");
sort($array1); echo "標(biāo)準(zhǔn)排序n"; print_r($array1);
natsort($array2); echo "n自然排序n"; print_r($array2);
代碼輸出為:
標(biāo)準(zhǔn)排序 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 )
|