|
|
 |
compact (PHP 4 , PHP 5) compact --
建立一个数组,包括变量名和它们的值
说明array compact ( mixed varname [, mixed ...])
compact() 接受可变的参数数目。每个参数可以是一个包括变量名的字符串或者是一个包含变量名的数组,该数组中还可以包含其它单元内容为变量名的数组,
compact() 可以递归处理。
对每个参数,compact()
在当前的符号表中查找该变量名并将它添加到输出的数组中,变量名成为键名而变量的内容成为该键的值。简单说,它做的事和
extract() 正好相反。返回将所有变量添加进去后的数组。
任何没有设定为变量名的字符串都被跳过。
例子 1. compact() 例子 |
<?php
$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
)
|
|
参见 extract()。
rlynch at ignitionstate dot com
27-Jan-2000 04:43
It should be noted that PHP will simply skip any strings that are not
set:
<?php
$foo = 4;
$bar = 3;
$result =
compact('foo', 'bar', 'baz');
?>
will result in:
('foo'
=> 4, 'bar' => 3)
'baz' is simply ignored.
| |