WebDesires - Web Development Cannock, West Midlands
Basket is empty

Merging Arrays Removing Unique Multi Dimensional Data

Need Some Help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request Support

There is a mobile optimized version of this page, view AMP Version.

Generally merging arrays in PHP is pretty straightforward given the built in functions on offer for developers.

<?php
    $array1 = array(0 => "data1", 1 => "data2", 3 => "data");
    $array2 = array(1 => "data");
    $result = array_merge($array1, $array2);
?>

Given the above array its obvious how simple merging 2 arrays can be. The expected result here is an array that appears as follows;

array(
    [0] => data1,
    [1] => data2,
    [2] => data,
    [3] => data,
)

This return may be what we’re after, however if you need to remove duplicate entries, in this case we have 2 instances where both values are “data”. We can use another PHP function called array_unique which filters through the array returning only unique values. There are 2 ways to do this, we could either run the function on the $result variable, or we could run it all in one line.

<?php
    $array1 = array(0 => "data1", 1 => "data2", 3 => "data");
    $array2 = array(1 => "data");
    $result = array_merge($array1, $array2);
    
    $result = array_unique($result);

    //All in one line
    $result = array_unique(array_merge($array1, $array2));
?>

Both of which will return an array looking like so;

array(
    [0] => data1,
    [1] => data2,
    [2] => data,
)

Perfect, removing duplicate array elements is also pretty easy. However, removing duplicate multidimensional array elements is a little trickier and requires some slight out of the box thinking. PHP’s array_unique() function only works on strings but we need to remove duplicate objects or multidimensional array elements.

First lets merge 2 multidimensional arrays;

<?php
    $cars1 = array (
        array("Volvo",22,18),
        array("BMW",15,13),
        array("Saab",5,2),
        array("Land Rover",17,15)
    );
    
    $cars2 = array(
        array("Volvo",22,18),
        array("Audi",7,11),
        array("Volkswagen",5,9),
    );
    
    $cars = array_merge($cars1, $cars2);
?>

Which then returns an array structure resembling this;

array (
    [0] => array
        (
            [0] => Volvo
            [1] => 22
            [2] => 18
        )

    [1] => array
        (
            [0] => BMW
            [1] => 15
            [2] => 13
        )

    [2] => array
        (
            [0] => Saab
            [1] => 5
            [2] => 2
        )

    [3] => array
        (
            [0] => Land Rover
            [1] => 17
            [2] => 15
        )

    [4] => array
        (
            [0] => Volvo
            [1] => 22
            [2] => 18
        )

    [5] => array
        (
            [0] => Audi
            [1] => 7
            [2] => 11
        )

    [6] => array
        (
            [0] => Volkswagen
            [1] => 5
            [2] => 9
        )
)

Quite clearly we have 2 identical arrays in here containing the values “Volvo”, 22 and 18. However we cannot use array_unique as previously mentioned, it only works on string data and not arrays or objects. However, if we convert the arrays into strings then we will be able to use array_unique() on the strings and remove any duplicates.

A simple one line solution to merging these arrays and removing duplicate data is as follows;

<?php
    $cars = array_map("unserialize", array_unique(array_map("serialize", array_merge($cars1, $cars2))));
?>

This smart little one liner first merges the 2 car arrays, it then maps each array into a serialized string, checks if those strings are unique and then maps the array back into an unserialized multi dimensional array without the duplicate data.

Need Some Help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request Support

Author: Josh Burley

Over 10 years software and website development, more recently moving into app development. Became a founding member and managing partner of WebDesires in late 2014 early 2015. Now dedicate most of my time to the company and helping us grow into the industry.


Leave a Reply