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

$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;

当前回答

为什么不使用

$appended = array_merge($a,$b); 

你为什么不想用这个,正确的内置方法。

其他回答

从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];

为什么不使用

$appended = array_merge($a,$b); 

你为什么不想用这个,正确的内置方法。

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

$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

希望能有所帮助。

Array_merge是一种优雅的方式

$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有相同的键,它什么都不会做。

<?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
)

参考源代码