(PHP 5)
array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
$keys
, array $values
)
返回一个 array,用来自
keys
数组的值作为键名,来自
values
数组的值作为相应的值。
如果两个数组的单元数不同或者数组为空时返回 FALSE
。
Example #1 简单的 array_combine() 例子
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
以上例程会输出:
Array ( [green] => avocado [red] => apple [yellow] => banana )
keys
Array of keys to be used. Illegal values for key will be converted to string.
values
Array of values to be used
Returns the combined array, FALSE
if the number of elements
for each array isn't equal.
Throws E_WARNING
if the number of elements in
keys
and values
does not
match.
版本 | 说明 |
---|---|
5.4.0 |
Previous versions issued E_WARNING and returned
FALSE for empty arrays.
|
Example #2 A simple array_combine() example
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
以上例程会输出:
Array ( [green] => avocado [red] => apple [yellow] => banana )