offline
offline

Reputation: 1619

How to merge multidimensional arrays in PHP to be able to output them together

I have made an example. We have two arrays : $users and $items. We need to merge them together so we can display them together. My question is how to do that ? I've made an example of how results should look like. In the following code you will see $merged1 and $merged2 arrays, that should represent the end goal. But how to achieve this ?

You can run this file on you server, lets call it index.php :

<!DOCTYPE html>
<html>
<head>
    <title>PHP arrays merge</title>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

</head>
<body>

    <div class="container"> <br>

        <?php 

            $users = [

                145 => ['first_name' => 'John', 'last_name' => 'Doe'],
                237 => ['first_name' => 'Erick', 'last_name' => 'Smith']

            ];

            $items = [

                145 => ['name' => 'Epic Sword', 'price' => 7000],
                237 => ['name' => 'Lame Sword', 'price' => 15]

            ];

            // how to merge $users and $items to get this array ?
            $merged1 = [
                145 => ['first_name' => 'John', 'last_name' => 'Doe', 'name' => 'Epic Sword', 'price' => 7000],
                237 => ['first_name' => 'Erick', 'last_name' => 'Smith', 'name' => 'Lame Sword', 'price' => 15]
            ];

            var_dump($merged1);

            echo "<br>";

            // how to merge $users and $items to get this array ?
            $merged2 = [

                145 => ['first_name' => 'John', 'last_name' => 'Doe', 
                        'items' => ['name' => 'Epic Sword', 'price' => 7000] // inner array
                ],

                237 => ['first_name' => 'Erick', 'last_name' => 'Smith',
                        'items' => ['name' => 'Lame Sword', 'price' => 15]

                ]

            ];

            var_dump($merged2);
        ?>

    </div>

<!-- jQuery library -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>

Upvotes: 2

Views: 83

Answers (1)

Skamielina
Skamielina

Reputation: 802

You should use function array_merge_recursive. Try this code:

$result = array_merge_recursive($users, $items);
print_r($result);

For 2nd result do:

$result2 = array();
foreach ($users as $key => $val) {
     $result2[$key] = array_merge((array)$val, array('items' => $items[$key]));
}

Edit:

For the first merge, according to solution phil dot kemmeter you can use following function:

function my_array_merge ($arr,$ins) {
    if(is_array($arr))
    {
        if(is_array($ins)) foreach($ins as $k=>$v)
        {
            if(isset($arr[$k])&&is_array($v)&&is_array($arr[$k]))
            {
                $arr[$k] = my_array_merge($arr[$k],$v);
            }
            else {
                // This is the new loop :)
                while (isset($arr[$k]))
                    $k++;
                $arr[$k] = $v;
            }
        }
    }
    elseif(!is_array($arr)&&(strlen($arr)==0||$arr==0))
    {
        $arr=$ins;
    }
    return($arr);
}
$result = my_array_merge($users, $items);
print_r($result);

Upvotes: 2

Related Questions