Creating a new array from multidimensional arrays is the next step

I looked for all the options for him, without success.

I have a multidimensional array like this:

Array 
(
[0] => Array
    (
        [0] => "Title1" 
        [1] => "Title2"
        [2] => "Title3"
    )
[1] => Array
    (
        [0] => "Title1"  
        [1] => "Title2"
    )
)
 Array #2

  Array
   (
[0] => Array
    (
        [0] => "Value1"  
        [1] => "Value2"
        [2] => "Value3"
    )
[1] => Array
    (
        [0] => "Value" 
        [1] => "Value2"
    )
)

And I would like to achieve this result:

New Array

Array
(
[0] => Array
    (
      [0] =>Array
          (
        [0] => "Title1"  
        [1] => "Title2"
        [2] => "Title3"
          )
          (
      [1] =>Array
          (
        [0] => "Title1"  
        [1] => "Title2"
          )
 )
 (

[1] => Array
    (
      [0] =>Array
          (
        [0] => "Value1"  
        [1] => "Value2"
        [2] => "Value3"
          )
          (
      [1] =>Array
          (
        [0] => "Value"  
        [1] => "Value2"
          )
    )
)

So, I want to add 1 level array. My way of thinking is to loop through a multidimensional array (2 loops) and add 3 of the next (in this example) array inside 3 loops. array_merge_recursive will not work. I tried to create the correct loop, but to no avail. Is it possible?

+4
source share
3 answers

You can do this as shown below ( Assign both arrays to a new array ): -

$arr3 = array($arr1,$arr2); // or [$arr1,$arr2];
print_r($arr3);

Exit: - https://eval.in/838283

+2

-

$finalArray=[$arr1,$arr2]

,

 $finalArray[] = $arr3;
+1

array_merge()

array_merge(array1,array2);
0

Source: https://habr.com/ru/post/1682401/


All Articles