PHP 8.4.2 Released!

compact

(PHP 4, PHP 5, PHP 7, PHP 8)

compact 建立一个数组,包括变量名和它们的值

说明

compact(array|string $var_name, array|string ...$var_names): array

创建一个包含变量与其值的数组。

对每个参数,compact() 在当前的符号表中查找该变量名并将它添加到输出的数组中, 变量名成为键名而变量的内容成为该键的值。简单说,它做的事和 extract() 正好相反。返回将所有变量添加进去后的数组。

注意:

在 PHP 7.3 之前版本,未设置的字符串会被静默忽略。

参数

var_name
var_names

compact() 接受可变的参数数量。每个参数不是包含变量名的字符串,就是变量名组成的数组。数组中可以包含由其他变量名组成的数组,compact() 会递归处理。

返回值

返回输出的数组,包含了添加的所有变量。

错误/异常

如果字符串指向的变量未定义,compact() 会产生 E_WARNING 级别的错误。

更新日志

版本 说明
8.0.0 如果指定字符串引用了未设置的变量,现在会发出 E_WARNING 级别的错误。
7.3.0 现在,如果字符串指向的变量未定义,compact() 会产生 E_NOTICE 级错误。在此之前,这样的字符串会默默地跳过。

示例

示例 #1 compact() 示例

<?php

$city
= "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$location_vars = array("city", "state");

$result = compact("event", $location_vars);
print_r($result);

?>

以上示例会输出:

Array
(
    [event] => SIGGRAPH
    [city] => San Francisco
    [state] => CA
)

注释

注意: Gotcha

因为可变变量也许不能在函数内部用于 PHP 的超全局数组,此时不能将超全局数组传递入 compact() 中。

参见

  • extract() - 从数组中将变量导入到当前的符号表

添加备注

用户贡献的备注 5 notes

up
170
M Spreij
17 years ago
Can also handy for debugging, to quickly show a bunch of variables and their values:

<?php
print_r
(compact(explode(' ', 'count acw cols coldepth')));
?>

gives

Array
(
[count] => 70
[acw] => 9
[cols] => 7
[coldepth] => 10
)
up
63
lekiagospel at gmail dot com
4 years ago
Consider these two examples. The first as used in the manual, and the second a slight variation of it.

Example #1

<?php
$city
= "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$location_vars = array("city", "state");

$result = compact("event", $location_vars);
print_r($result);
?>

Example #1 above will output:

Array
(
[event] => SIGGRAPH
[city] => San Francisco
[state] => CA
)

Example #2

<?php
$city
= "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$location_vars = array("city", "state");

$result = compact("event", "location_vars");
print_r($result);
?>

Example #2 above will output:

Array
(
[event] => SIGGRAPH

[location_vars] => Array
(
[0] => city
[1] => state
)

)

In the first example, the value of the variable $location_values (which is an array containing city, and state) is passed to compact().

In the second example, the name of the variable $location_vars (i.e without the '$' sign) is passed to compact() as a string. I hope this further clarifies the points made in the manual?
up
56
jmarkmurph at yahoo dot com
8 years ago
So compact('var1', 'var2') is the same as saying array('var1' => $var1, 'var2' => $var2) as long as $var1 and $var2 are set.
up
2
c dot smith at fantasticmedia dot co dot uk
1 year ago
If you must utilise this knowing that a variable may be unset, then you need to use an alternative method.

So instead of the following:

<?php
$var1
= "lorem";
$var2 = "ipsum";
$result = compact('var1', 'var2', 'unsetvar');
?>

Consider the following:

<?php
$var1
= "lorem";
$var2 = "ipsum";
$result = [];
foreach( [
'var1', 'var2', 'unsetvar'] as $attr ) {
if ( isset( $
$attr ) ) {
$result[ $attr ] = $$attr;
}
}
?>
up
28
Robc
13 years ago
The description says that compact is the opposite of extract() but it is important to understand that it does not completely reverse extract(). In particluar compact() does not unset() the argument variables given to it (and that extract() may have created). If you want the individual variables to be unset after they are combined into an array then you have to do that yourself.
To Top