明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

介紹幾個 php4 中10分實用的數(shù)組函數(shù)(轉載)關聯(lián)數(shù)組等同于PERL里的哈希數(shù)組。以前我一直以為PHP里沒...

[摘要]介紹幾個 php4 中非常有用的"數(shù)組"函數(shù)1 void extract (array var_array [, int extract_type ][, string pref...
介紹幾個 php4 中非常有用的"數(shù)組"函數(shù)

1 void extract (array var_array [, int extract_type ][, string prefix]])
把一個關聯(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ù)相反,把變量名和變量的值保存到關聯(lián)數(shù)組里面!
如:
$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)
判斷數(shù)組中是否有這個值

4 void natsort (array array)
以自然數(shù)的方法排序數(shù)組,這時 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