PHP 将一个数组附加到另一个数组(不是 array_push 或 +) [英] PHP append one array to another (not array_push or +)
本文介绍了PHP 将一个数组附加到另一个数组(不是 array_push 或 +)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将一个数组附加到另一个数组而不比较它们的键?
How to append one array to another without comparing their keys?
$a = array( 'a', 'b' );
$b = array( 'c', 'd' );
最后应该是:Array( [0]=>a [1]=>b [2]=>c [3]=>d)
如果我使用 []
或 array_push
之类的东西,它将导致以下结果之一:
At the end it should be: Array( [0]=>a [1]=>b [2]=>c [3]=>d )
If I use something like []
or array_push
, it will cause one of these results:
Array( [0]=>a [1]=>b [2]=>Array( [0]=>c [1]=>d ) )
//or
Array( [0]=>c [1]=>d )
它应该是一些东西,这样做,但要以更优雅的方式:
It just should be something, doing this, but in a more elegant way:
foreach ( $b AS $var )
$a[] = $var;
推荐答案
array_merge
是一种优雅的方式:
array_merge
is the elegant way:
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b);
// $merge is now equals to array('a','b','c','d');
做类似的事情:
$merge = $a + $b;
// $merge now equals array('a','b')
不会工作,因为 +
操作符实际上并没有合并它们.如果它们 $a
和 $b
有相同的键,它不会做任何事情.
Will not work, because the +
operator does not actually merge them. If they $a
has the same keys as $b
, it won't do anything.
这篇关于PHP 将一个数组附加到另一个数组(不是 array_push 或 +)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文