[英]Merge two arrays in PHP
I have two arrays: 我有两个数组:
$a = array([0]=>1 [1]=>2 [2]=>3);
$b = array([0]=>a [1]=>b [2]=>c);
I want to merge them like 我想像合并它们
$ab=array( [a]=>array([0]=>1 [1]=>2 [2]=>3)
[b]=>array([0]=>a [1]=>b [2]=>c) );
How to do this ? 这个怎么做 ?
I tried array_merge()
but it does not work as I want it to. 我尝试了
array_merge()
但是它并没有按照我的意愿工作。
在这种情况下,您可以执行以下操作:
$ab = array('a' => $a, 'b' => $b);
You can use compact()
method: 您可以使用
compact()
方法:
$ab = compact('a', 'b');
where 'a'
and 'b'
will used as array keys and treated as variable names to assign values, so it will do array('a' => $a, 'b' => $b)
其中
'a'
和'b'
将用作数组键并被视为变量名称以分配值,因此它将执行array('a' => $a, 'b' => $b)
您已经写了答案了。
$ab = array('a' => $a, 'b' => $b);
$ab = array('a' => $a, 'b' => $b);
Will result in: 将导致:
$ab['a'] = array([0] => 1, [1] => 2, [2] => 3);
$ab['b'] = array([0] => a, [1] => b, [2] => c);
Or you can just do $ab = array($a, $b)
if you don't want the keys (it is unclear in your question if a and b are strings or integers). 或者
$ab = array($a, $b)
如果您不想要键$ab = array($a, $b)
也可以只执行$ab = array($a, $b)
(不清楚a和b是字符串还是整数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.