php数组追加⼀个数组,PHP追加⼀个数组到另⼀个(⽽不是
array_push或+)
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有相同的密钥,它将不会执⾏任何操作。
为什么不使⽤
$appended = array_merge($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 " Example 1 Output:
"; 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 "
Example 2 Output:
"; 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 "
Example 3 Output:
"; 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 => "
php8兼容php7吗
CSS",4 => "CSS3"); echo "
Example 4 Output:
"; 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 )
参考源代码
这是⼀个很⽼的post,但我想添加⼀些附加到另⼀个数组:
如果
⼀个或两个数组都有关联键
两个数组的键都⽆关紧要
你可以使⽤这样的数组函数:
array_merge(array_values($array), array_values($appendArray));
array_merge不合并数字键,所以它附加$ appendArray的所有值。 当使⽤本地php函数⽽不是foreach循环时,对于有很多元素的数组,它应该会更快。
在PHP 5.6+中执⾏此操作的另⼀种⽅法是使⽤...标记
$a = array('a', 'b'); $b = array('c', 'd'); array_push($a, ...$b); // $a is now equals to array('a','b','c','d');
这也适⽤于任何Traversable
$a = array('a', 'b'); $b = new ArrayIterator(array('c', 'd')); array_push($a, ...$b); // $a is now equals to array('a','b','c','d');
警告虽然,如果数组$b是空的,这将导致⼀个致命的错误
对于⼤数组,最好是在没有array_merge的情况下进⾏连接,以避免内存复制。
$array1 = array_fill(0,50000,'aa'); $array2 = array_fill(0,100,'bb'); // Test 1 (array_merge) $start = micr
otime(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
从bstoney和Snark的回答开始,我对各种⽅法做了⼀些testing:
$array1 = array_fill(0,50000,'aa'); $array2 = array_fill(0,50000,'bb'); // Test 1 (array_merge) $start = microtime(true); $array1 = array_merge($array1, $array2); echo sprintf("Test 1: %.06f\n", microtime(true) - $start); // Test2 (foreach) $start = microtime(true); foreach ($array2 as $v) { $array1[] = $v; } echo sprintf("Test 2: %.06f\n", microtime(true) - $start); // Test 3 (... token) // PHP 5.6+ and produces error if $array2 is empty $start = microtime(true); array_push($array1,
...$array2); echo sprintf("Test 3: %.06f\n", microtime(true) - $start);
其中产⽣:
Test 1: 0.008392 Test 2: 0.004626 Test 3: 0.003574
我相信从PHP 7开始,⽅法3是⼀个⾮常好的select,因为现在foreach循环的⽅式就是迭代数组的副本。
虽然⽅法3并不严格地回答“not array_push”这个问题的标准,但在所有⽅⾯都是⼀条线和最⾼的性能,我认为这个问题在…语法是⼀个选项之前就被问到了。
foreach循环⽐array_merge快于将数值附加到现有数组,所以如果要将数组添加到另⼀个数组的末尾,请select循环。
// Create an array of arrays $chars = []; for ($i = 0; $i < 15000; $i++) { $chars[] = array_fill(0, 10, 'a'); } // test array_merge $new = []; $start = microtime(TRUE); foreach ($chars as $splitArray) { $new = array_merge($new, $splitArray); } echo microtime(true) - $start; // => 14.61776 sec // test foreach $new = []; $start = microtime(TRUE); foreach ($chars as $splitArray) { foreach ($splitArray as $value) { $new[] = $value; } } echo microtime(true) - $start; // => 0.00900101 sec // ==> 1600 times faster
如果你想合并空数组和现有的新值。 你必须先初始化它。
$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
希望它的帮助。
在PHP7之前,您可以使⽤:
array_splice($a, count($a), 0, $b);
$a = array("a", "b"); $b = array("c", "d"); $a = implode(",", $a); $b = implode(",", $b); $c = $a . "," . $b; $c = explode(",", $c);
这个怎么样:
$appended = $a + $b;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。