skip to Main Content

I want to need multiple array combaine in one array

I have array

array(
  0=> test 1
)
array(
  0=> test 2
)
array(
  0=> test 3
)

I need expected output

`array(
 0=>Test1
1=>Test2
2=>test3

)`

3

Answers


  1. You can use the array_merge() for this. The array_merge() function merges one or more arrays into one array.If two or more array elements have the same key, the last one overrides the others.

    Syntax:

    array_merge(array ...$arrays): array
    

    Example:

    $a1=array("red","green");
    $a2=array("blue","yellow");
    print_r(array_merge($a1,$a2));
    

    Result:

    Array ( [0] => red [1] => green [2] => blue [3] => yellow )
    

    You can check more here.

    Login or Signup to reply.
  2. $a = array('test_1');
    $b = array('test_2');
    $c = array('test_3');
    print_r(array_merge($a,$b,$c));
    

    O/P – Array ( [0] => test_1 [1] => test_2 [2] => test_3 )

    Login or Signup to reply.
  3. Hope you are doing well and good.
    So, as per your requirement i found solution to get result.

    $array1 = [0 => "Test 1"]; $array2 = [0 => "Test 2"]; $array3 = [0 => "Test 3"];

    print_r(array_merge($array1,$array2,$array3));

    In the above example you have to merge the n number of array with single array, so for that you need to use array function which is array_merge(array …$array).

    What is array_merge()?

    The array_merge() function merges one or more arrays into one array.

    Tip: You can assign one array to the function, or as many as you like.

    Note: If two or more array elements have the same key, the last one overrides the others.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search