![]() |
奔跑的书包 · 国家版权局-打击侵权盗版典型案件-四川发布2 ...· 4 月前 · |
![]() |
笑点低的肉夹馍 · 奥运会摔跤项目亚洲区资格赛结束——中国摔跤队 ...· 5 月前 · |
![]() |
英姿勃勃的针织衫 · Translation dosen't ...· 6 月前 · |
![]() |
彷徨的香菇 · 清朝铜钱价格排名-西瓜视频搜索· 6 月前 · |
![]() |
大鼻子的鸡蛋 · 哈市教育局发布 | 哈尔滨市“十百千” ...· 6 月前 · |
如何在不比较数组键的情况下将一个数组追加到另一个数组?
$a = array( 'a', 'b' );
$b = array( 'c', 'd' );
最后应该是:
Array( [0]=>a [1]=>b [2]=>c [3]=>d )
如果我使用像
[]
或
array_push
这样的东西,它将导致以下结果之一:
Array( [0]=>a [1]=>b [2]=>Array( [0]=>c [1]=>d ) )
Array( [0]=>c [1]=>d )
它应该是某种东西,这样做,但以一种更优雅的方式:
foreach ( $b AS $var )
$a[] = $var;
这样如何:
$appended = $a + $b;
为什么不使用
$appended = array_merge($a,$b);
你为什么不使用这个,正确的,内置的方法。
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
具有相同的密钥,它将不会执行任何操作。
对于大型数组,最好不使用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
在bstoney和Snark的回答之后,我对各种方法进行了一些测试:
// Test 1 (array_merge)
$array1 = $array2 = array_fill(0, 50000, 'aa');
$start = microtime(true);
$array1 = array_merge($array1, $array2);
echo sprintf("Test 1: %.06f\n", microtime(true) - $start);
// Test2 (foreach)
$array1 = $array2 = array_fill(0, 50000, 'aa');
$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
$array1 = $array2 = array_fill(0, 50000, 'aa');
$start = microtime(true);
array_push($array1, ...$array2);
echo sprintf("Test 3: %.06f\n", microtime(true) - $start);
这会产生:
Test 1: 0.002717
Test 2: 0.006922
Test 3: 0.004744
原始:我认为在PHP7中,方法3是一种明显更好的替代方法,这是由于 foreach loops now act 的方式,即对要迭代的数组进行复制。
虽然方法3没有严格地回答问题中的“not array_push”标准,但它是一行,并且在所有方面都是最高性能的,我认为这个问题是在...语法是一种选择。
UPDATE 25/03/2020:我更新了测试,因为变量没有重置,所以测试有缺陷。有趣的(或令人困惑的)结果现在显示测试1是最快的,它是最慢的,从0.008392到0.002717!这只能归结到PHP更新,因为这不会受到测试缺陷的影响。
所以,传奇还在继续,我将从现在开始使用array_merge!
如果要将空数组与现有新值合并。您必须先对其进行初始化。
$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);
array_splice()
根据数组(第一个参数)进行操作,并将数组(第四个参数)值放在从第二个参数开始的值列表和第三个参数的数量的位置。当我们将第二个参数设置为源数组的末尾,将第三个参数设置为零时,我们将第四个参数的值附加到第一个参数
在将值追加到现有数组中时,foreach循环比array_merge更快,因此,如果要将一个数组添加到另一个数组的末尾,请选择该循环。
// 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) {
![]() |
英姿勃勃的针织衫 · Translation dosen't work in Microsoft 365 (Document Translation Failed .Please Try again) - Microsof 6 月前 |
![]() |
彷徨的香菇 · 清朝铜钱价格排名-西瓜视频搜索 6 月前 |