[英]Easy way to create an array in PHP
I have 2 arrays:我有 2 个 arrays:
I would like to merge them into an associated array in PHP.我想将它们合并到 PHP 中的关联数组中。
Is there a simpler way to do this other than using loops?除了使用循环之外,还有更简单的方法吗?
Use array_combine()
function:使用
array_combine()
function:
http://php.net/manual/en/function.array-combine.php http://php.net/manual/en/function.array-combine.php
Snippet:片段:
$keys = array('a', 'b', 'c', 'd');
$values = array(1, 2, 3, 4);
$result = array_combine($keys, $values);
var_dump($result);
Result:结果:
array(4) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
}
Use array_combine
使用
array_combine
Example for docs:文档示例:
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
Should output:应 output:
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)
Check out http://php.net/manual/en/function.array-combine.php查看http://php.net/manual/en/function.array-combine.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.