如何追加一个数组到另一个没有比较他们的键?

$a = array( 'a', 'b' );
$b = array( 'c', 'd' );

最后应该是:阵列([0]= > [1]= > b [2] = > c [3] = > d) 如果我使用类似[]或array_push的东西,它会导致以下结果之一:

Array( [0]=>a [1]=>b [2]=>Array( [0]=>c [1]=>d ) )
//or
Array( [0]=>c [1]=>d )

它应该是这样做的,但是以一种更优雅的方式:

foreach ( $b AS $var )
    $a[] = $var;

当前回答

对于大数组,最好不使用array_merge进行连接,以避免内存复制。

$array1 = array_fill(0,50000,'aa');
$array2 = array_fill(0,100,'bb');

// Test 1 (array_merge)
$start = microtime(true);
$r1 = array_merge($array1, $array2);
echo sprintf("Test 1: %.06f\n", microtime(true) - $start);

// Test2 (avoid copy)
$start = microtime(true);
foreach ($array2 as $v) {
    $array1[] = $v;
}
echo sprintf("Test 2: %.06f\n", microtime(true) - $start);


// Test 1: 0.004963
// Test 2: 0.000038

其他回答

这是一篇相当老的文章,但我想添加一些关于将一个数组附加到另一个数组的东西:

If

一个或两个数组都有关联键 两个数组的键都不重要

你可以像这样使用数组函数:

array_merge(array_values($array), array_values($appendArray));

array_merge不合并数字键,所以它附加$appendArray的所有值。当使用原生php函数而不是foreach-loop时,它应该在包含大量元素的数组上更快。

除了2019-12-13: 从PHP 7.4开始,就有可能以数组展开操作符的方式追加或前置数组:

    $a = [3, 4];
    $b = [1, 2, ...$a];

和以前一样,这个新功能的键可能是一个问题:

    $a = ['a' => 3, 'b' => 4];
    $b = ['c' => 1, 'a' => 2, ...$a];

"致命错误:未捕获错误:无法解包包含字符串键的数组"

    $a = [3 => 3, 4 => 4];
    $b = [1 => 1, 4 => 2, ...$a];

阵列(4){ [1] = > int (1) [4] = > int (2) [5] = > int (3) [6] = > int (4) }

    $a = [1 => 1, 2 => 2];
    $b = [...$a, 3 => 3, 1 => 4];

数组(3){ [0] = > int (1) [1] = > int (4) [3] = > int (3) }

<?php
// Example 1 [Merging associative arrays. When two or more arrays have same key
// then the last array key value overrides the others one]

$array1 = array("a" => "JAVA", "b" => "ASP");
$array2 = array("c" => "C", "b" => "PHP");
echo " <br> Example 1 Output: <br>";
print_r(array_merge($array1,$array2));

// Example 2 [When you want to merge arrays having integer keys and
//want to reset integer keys to start from 0 then use array_merge() function]

$array3 =array(5 => "CSS",6 => "CSS3");
$array4 =array(8 => "JAVASCRIPT",9 => "HTML");
echo " <br> Example 2 Output: <br>";
print_r(array_merge($array3,$array4));

// Example 3 [When you want to merge arrays having integer keys and
// want to retain integer keys as it is then use PLUS (+) operator to merge arrays]

$array5 =array(5 => "CSS",6 => "CSS3");
$array6 =array(8 => "JAVASCRIPT",9 => "HTML");
echo " <br> Example 3 Output: <br>";
print_r($array5+$array6);

// Example 4 [When single array pass to array_merge having integer keys
// then the array return by array_merge have integer keys starting from 0]

$array7 =array(3 => "CSS",4 => "CSS3");
echo " <br> Example 4 Output: <br>";
print_r(array_merge($array7));
?>

输出:

Example 1 Output:
Array
(
[a] => JAVA
[b] => PHP
[c] => C
)

Example 2 Output:
Array
(
[0] => CSS
[1] => CSS3
[2] => JAVASCRIPT
[3] => HTML
)

Example 3 Output:
Array
(
[5] => CSS
[6] => CSS3
[8] => JAVASCRIPT
[9] => HTML
)

Example 4 Output:
Array
(
[0] => CSS
[1] => CSS3
)

参考源代码

从PHP 7.4开始,你可以使用…操作符。这在其他语言(包括Ruby)中也称为splat操作符。

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
var_dump($fruits);

输出

array(5) {
    [0]=>
    string(6) "banana"
    [1]=>
    string(6) "orange"
    [2]=>
    string(5) "apple"
    [3]=>
    string(4) "pear"
    [4]=>
    string(10) "watermelon"
}

Splat操作符应该比array_merge有更好的性能。这不仅是因为splat操作符是一种语言结构,而array_merge是一个函数,而且还因为编译时优化可以用于常量数组。

此外,我们可以在数组中的任何地方使用splat操作符语法,因为普通元素可以添加在splat操作符之前或之后。

$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];
$arr3 = [...$arr1, ...$arr2];
$arr4 = [...$arr1, ...$arr3, 7, 8, 9];

对于大数组,最好不使用array_merge进行连接,以避免内存复制。

$array1 = array_fill(0,50000,'aa');
$array2 = array_fill(0,100,'bb');

// Test 1 (array_merge)
$start = microtime(true);
$r1 = array_merge($array1, $array2);
echo sprintf("Test 1: %.06f\n", microtime(true) - $start);

// Test2 (avoid copy)
$start = microtime(true);
foreach ($array2 as $v) {
    $array1[] = $v;
}
echo sprintf("Test 2: %.06f\n", microtime(true) - $start);


// Test 1: 0.004963
// Test 2: 0.000038

如果您想合并空数组与现有的新值。您必须先初始化它。

$products = array();
//just example
for($brand_id=1;$brand_id<=3;$brand_id++){
  array_merge($products,getByBrand($brand_id));
}
// it will create empty array
print_r($a);

//check if array of products is empty
for($brand_id=1;$brand_id<=3;$brand_id++){
  if(empty($products)){
    $products = getByBrand($brand_id);
  }else{
    array_merge($products,getByBrand($brand_id));
  }
}
// it will create array of products

希望能有所帮助。