創(chuàng)建一個包含變量名和它們的值的數(shù)組:
<?php
$firstname = "Bill";
$lastname = "Gates";
$age = "60";
$result = compact("firstname", "lastname", "age")
;
print_r($result);
?>
compact() 函數(shù)創(chuàng)建包含變量名和它們的值的數(shù)組。
注釋:任何沒有變量名與之對應的字符串都被略過。
compact(var1,var2...)
參數(shù) | 描述 |
---|---|
var1 | 必需。可以是帶有變量名的字符串,或者是變量數(shù)組。 |
var2,... | 可選??梢允菐в凶兞棵淖址?,或者是變量數(shù)組。允許多個參數(shù)。 |
compact() 函數(shù)創(chuàng)建一個由參數(shù)所帶變量組成的數(shù)組。如果參數(shù)中存在數(shù)組,該數(shù)組中變量的值也會被獲取。
本函數(shù)返回的數(shù)組是一個關(guān)聯(lián)數(shù)組,鍵名為函數(shù)的參數(shù),鍵值為參數(shù)中變量的值。
本函數(shù)執(zhí)行的行為與 extract() 正好相反。
返回值: | 返回帶有所有變量名和它們的值的數(shù)組。 |
PHP 版本: | 4+ |
使用不匹配變量的字符串,以及一個變量名數(shù)組:
<?php
$firstname = "Bill";
$lastname = "Gates";
$age = "60";
$name = array("firstname", "lastname");
$result = compact($name, "location", "age")
;
print_r($result);
?>
如對本文有疑問,請?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會為你解答??! 點擊進入論壇
Powered by 365建站網(wǎng) RSS地圖 HTML地圖
copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號