PHP Remove duplicates from multidimensional array Example

In this article, we will learn how to remove duplicates from multidimensional array in PHP, remove duplicate elements from array in PHP without using function, PHP remove duplicates from multidimensional array by key, and also we will learn to find duplicate values in array PHP , PHP find duplicate values in associative array, PHP find duplicates in multidimensional array.

How to remove duplicates from multidimensional array in PHP ?

To remove duplicates from multidimensional array in php, you can use the following most common ways:

Method 1: Using array_map and json_encode

One way to remove duplicates from multidimensional arrays is to use the combination of array_map and json_encode functions. This method works by converting each nested array into a JSON-encoded string, then removing duplicates based on the unique JSON strings.

Here’s an example code snippet:

// Define a sample multidimensional array with duplicates
$array = array(
  array('id' => 1, 'name' => 'John'),
  array('id' => 2, 'name' => 'Doe'),
  array('id' => 1, 'name' => 'John'),
  array('id' => 3, 'name' => 'Jane')
);

// Remove duplicates using array_map and json_encode
$array = array_map('json_decode', array_unique(array_map('json_encode', $array)));

// Print the updated array
print_r($array);

In the above code, we first define our multidimensional array with duplicates. Then, we use array_map to apply the serialize function to each nested array, which converts it into a JSON-encoded string. Next, we use array_unique to remove duplicates based on these unique strings. Finally, we use array_map again, this time applying the unserialize function to each array to convert it back to its original format.

Here’s output of above given example:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John
        )

    [1] => Array
        (
            [id] => 2
            [name] => Doe
        )

    [3] => Array
        (
            [id] => 3
            [name] => Jane
        )

)

Method 2: Using a loop and in_array

Another method to remove duplicates from a multidimensional array is by using a loop and the in_array function. Here’s an example code snippet that demonstrates how to use this method:

// Define a sample multidimensional array with duplicates
$array = array(
  array('id' => 1, 'name' => 'John'),
  array('id' => 2, 'name' => 'Doe'),
  array('id' => 1, 'name' => 'John'),
  array('id' => 3, 'name' => 'Jane')
);

// Remove duplicates using a loop and in_array
$unique_array = array();
foreach($array as $sub_array) {
  if(!in_array($sub_array, $unique_array)) {
    $unique_array[] = $sub_array;
  }
}

// Print the updated array
print_r($unique_array);

Here’s output of above given example:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John
        )

    [1] => Array
        (
            [id] => 2
            [name] => Doe
        )

    [3] => Array
        (
            [id] => 3
            [name] => Jane
        )

)

In this method, we use a loop to iterate through each sub-array of the multidimensional array. We then check if the sub-array exists in the new unique array using the in_array function. If the sub-array does not exist, we add it to the unique array.

Method 3: Remove Duplicate values from multidimensional array using function

Here we will take an example to remove duplicate elements or values from a multidimensional array using PHP functions.

Array
(
    [0] => Array
        (
            [0] => laravel
            [1] => php
        )
    [1] => Array
        (
            [0] => codeigniter
            [1] => php
        )
    [2] => Array
        (
            [0] => laravel
            [1] => php
        )
    [3] => Array
        (
            [0] => c++
            [1] => java
        )
)
<?php
$arr = Array(
          Array('laravel','php'),
          Array('codeigniter','php'),
          Array('laravel','php'),
          Array('c++','java')
        );
$arr = array_map("unserialize", array_unique(array_map("serialize", $arr)));
print_r($arr);
?>

Method 4: PHP remove duplicates from the multidimensional array by key-value

Suppose you have one array look like this:

Array (
    [0] => Array ( [name] => Apple [amount] => 1)
    [1] => Array ( [name] => Apple [amount] => 2)
    [2] => Array ( [name] => Orange [amount] => 3)
    [3] => Array ( [name] => Orange [amount] => 4)
    [4] => Array ( [name] => Banana [amount] => 5)
    [5] => Array ( [name] => Pear [amount] => 6)
)

You want to remove duplicate values/elements from the multi dimensional array by key value. You can use the below code for remove multi demsional array by key value:

<?php
$orgArray = array(
    array ('name' => 'Apple', 'amount' => 1),
    array ('name' => 'Apple', 'amount' => 2),
    array ('name' => 'Orange', 'amount' => 3),
    array ('name' => 'Orange', 'amount' => 4),
    array ('name' => 'Banana', 'amount' => 5),
    array ('name' => 'Pear', 'amount' => 6),
);
$newArray = array();
$Fruits = array();
foreach ( $orgArray AS $key => $line ) {
    if ( !in_array($line['name'], $Fruits) ) {
        $Fruits[] = $line['name'];
        $newArray[$key] = $line;
    }
}
$originalArray = $newArray;
$newArray = NULL;
$Fruits = NULL;
print_r($orgArray);
?>

Check for duplicate values in a multidimensional array

<?php
  $data = Array(
    0 => Array("a", "b", "c"),
    1 => Array("x", "y", "z"),
    2 => Array("a", "b", "c"),
    3 => Array("a", "b", "c"),
    4 => Array("a", "x", "z"),
);
$final = array();
foreach ($data as $array) {
    if(!in_array($array, $final)){
        $final[] = $array;
    }
}
print_r($final);
?>

Recommended PHP Tutorials

  1. PHP Array: Indexed,Associative, Multidimensional
  2. To Remove Elements or Values from Array PHP
  3. Remove Duplicate Elements or Values from Array PHP
  4. How to Convert String to Array in PHP
  5. Array Push and POP in PHP | PHP Tutorial
  6. PHP Search Multidimensional Array [key and value and return key]
  7. PHP Array to String Conversion – PHP Implode
  8. Array Functions In PHP – PHP Tutorial

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *