介紹幾個 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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.