将多个数组合并为单个数组[关闭]

问题描述 投票:-5回答:4

如何合并下面的数组

 Array
(
    [0] => 14
)
Array
(
    [0] => 18
)

并获得输出

    Array
(
    [0] => 14
    [1] => 18

)

我使用下面的代码但不适合我

$result = call_user_func_array("array_merge", $input);
php arrays
4个回答
1
投票

使用array_merge方法:http://php.net/manual/en/function.array-merge.php

<?php

$array = array("1" );
$otherArray = array("2");

$result = array_merge($array, $otherArray);

print_r($result);

看现场代码:http://sandbox.onlinephpfunctions.com/code/7fb9da6de4fa9d0a8b220a06d2a59e9007655df1


0
投票

肯定只是array_merge就足够了?

    $a=[14];
    $b=[18];
    $c=[23];
    $d=[44];
    $e=[27];
    $f=[31];
    $g=[99];


    $out=array_merge($a,$b,$c,$d,$e,$f,$g);
    printf('<pre>%s</pre>',print_r($out,true));

这将输出:

Array
(
    [0] => 14
    [1] => 18
    [2] => 23
    [3] => 44
    [4] => 27
    [5] => 31
    [6] => 99
)

0
投票

OP询问是否合并来自一个数组的数组。我们不知道那里有多少个数组,所以我们需要使用array_walk_recursive函数。在匿名函数中通过引用(!)传递来自公共范围的$ result,并将所有内部数组值存储在其中

$a = [[1], [2, 3]];

$result = [];
array_walk_recursive($a, function($value) use (&$result){
    $result[] = $value;
});
var_dump($result);

-1
投票

为什么call_user_func?

$a = array(14);
$b = array(18);
$merged = array_merge($a,$b);
© www.soinside.com 2019 - 2024. All rights reserved.