Merge multiple arrays from one array

前端 未结 7 1868
臣服心动
臣服心动 2020-12-11 06:03

How to merge multiple arrays from a single array variable ? lets say i have this in one array variable

Those are in one variable ..
$array

相关标签:
7条回答
  • 2020-12-11 06:39

    This is the PHP equivalent of javascript Function#apply (generate an argument list from an array):

    $result = call_user_func_array("array_merge", $input);
    

    demo: http://3v4l.org/nKfjp

    0 讨论(0)
  • 2020-12-11 06:45

    Since PHP 5.6 you can use variadics and argument unpacking.

    $result = array_merge(...$input);
    

    It's up to 3x times faster than call_user_func_array.

    0 讨论(0)
  • 2020-12-11 06:50
    $arr1 = array(0=>1);
    $arr2 = array(0=>2);
    
    $merged = array_merge($arr1,$arr2);
    print_r($merged);
    
    0 讨论(0)
  • 2020-12-11 06:50

    $resultArray = array_merge ($array1, $array1);

    $result = array();
    foreach ($array1 as $subarray) {
        $result = array_merge($result, $subarray);
    }
    
    // Here it is done
    

    Something good to read: http://ca2.php.net/manual/en/function.array-merge.php

    Recursive:

    http://ca2.php.net/manual/en/function.array-merge-recursive.php

    0 讨论(0)
  • 2020-12-11 06:54

    This may work:

    $array1 = array("item1" => "orange", "item2" => "apple", "item3" => "grape");
    $array2 = array("key1" => "peach", "key2" => "apple", "key3" => "plumb");
    $array3 = array("val1" => "lemon");
    
    $newArray = array_merge($array1, $array2, $array3);
    
    foreach ($newArray as $key => $value) {
      echo "$key - <strong>$value</strong> <br />"; 
    }
    
    0 讨论(0)
  • 2020-12-11 06:54

    array_merge can do the job

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

    after the comment

    If fixed indexs you can use:

    $array_meged = array_merge($a[0], $a[1]);
    

    A more generic solution:

    $array_meged=array();
      foreach($a as $child){
      $array_meged += $child;
    }
    
    0 讨论(0)
提交回复
热议问题