0

We have this arrays,

<?php
    $arr1=array(0,1,2,3);
    $arr2=array(array(0,1,2,3));

    array_push($arr1,$arr2);
    echo "<pre>";
    print_r($arr1);
?>

Output :

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => Array
        (
            [0] => Array
                (
                    [0] => 0
                    [1] => 1
                    [2] => 2
                    [3] => 3
                )

        )

)

Expected :

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 0
    [5] => 1
    [6] => 2
    [7] => 3
)

We have tried array_push($arr1,$arr2)

5 Answers 5

2

There is no built-in functionality for that. However, you can easily do it with recursive functions or with recursive iterators, for example this:

$arrayIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array2));
foreach($arrayIterator as $value) {
    $array1[] = $value;
}

it can be done without foreach too:

$flatArray = iterator_to_array(
    new RecursiveIteratorIterator(new RecursiveArrayIterator($array2)),
    false
);
$array1 = array_merge($array1, $flatArray);
Sign up to request clarification or add additional context in comments.

1 Comment

thanx alot! appreciative answer!!
1

Solution for "dynamic" multidimensional arrays with array_merge_recursive and array_walk_recursive functions:

$arr1 = array(0,1,2,3);
$arr2 = array(array(0,1,2,3), array(0 => array(4,5), 2));
$merged = array_merge_recursive($arr1,$arr2);

$result = [];
array_walk_recursive($merged, function($v,$k) use(&$result){
    $result[] = $v;
});
var_dump($result);

The output:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 0
    [5] => 1
    [6] => 2
    [7] => 3
    [8] => 4
    [9] => 5
    [10] => 2
)

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

1 Comment

thanx alot! appreciative answer!!
1

Try this :

$arr1=array(0,1,2,3);
$arr2=array(array(0,1,2,3));

$result = array_merge($arr1, $arr2[0]);
echo "<pre>";
print_r($result);

You can get this with a long way also :

for($i=0;$i<count($arr2[0]);$i++)
{
    array_push($arr1,$arr2[0][$i]);
}
echo "<pre>"; 
print_r($arr1);

2 Comments

the way is good, but its not feasible for dynamically multiple arrays
Yes I know, my answer is as per the example provided in question. Both arrays can be dynamic or only $arr2 ?
0

array_merge($arr1,$arr2); will work.

Comments

0

I wrote function that gets values from any multidimensional array and I used it in creating result array:

<?php

 $arr1=array(0,1,2,3);
 $arr2=array(array(0,1,2,3));
 $arr3=array(array(array(0,1,2,3)));


function getValues($array)
{
    $result = array();

    if(isset($array[0]) && is_array($array[0]))
       $result = getValues($array[0]);
    else
    {
        $result = $array;
    }

    return $result;   
}


$arr1Values = getValues($arr1);
$arr2Values = getValues($arr2);
$arr3Values = getValues($arr3);

$result = $arr1Values;

foreach($arr2Values as $value)
{
    $result[] = $value;
}

foreach($arr3Values as $value)
{
    $result[] = $value;
}

echo '<pre>';
print_r($result);
echo '</pre>';

Result is:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 0
    [5] => 1
    [6] => 2
    [7] => 3
    [8] => 0
    [9] => 1
    [10] => 2
    [11] => 3
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.