Php

Remove Duplicates From Multidimensional Array

In this post, I am going to explain how to remove duplicate values from multidimensional array in PHP. I use array_unique() to get unique but its work on single-dimensional array, not able to work on multidimensional arrays.

You can remove duplicates from the multidimensional array by value in PHP.

I found answers to find a unique multidimensional array after a lot of googling. As a result, we’ll use array serialization for this. Serialization is a technique for storing and transmitting PHP values without compromising their type or structure.

Checkout other tutorial of PHP Array,

array_unique in PHP

The array_unique() function are used to remove duplicate data from given array.

Syntax:
array_unique(array, sorttype)

There are two parameters that will be passed in the array_unique(), first specifying an array that is required and the second is optional specifying the sorting type.

Optional parameters have the following possible values :

  • SORT_STRING – Default
  • SORT_NUMERIC
  • SORT_REGULAR
  • SORT_LOCALE_STRING

Remove duplicate values from an array in PHP

Let’s remove duplicate values from an array using PHP array_unique() method.

<?php
$reqArray=array("name"=-->"adam","sub"=>"english","sub"=>"french","sub"=>"english");
echo "<pre>";
print_r(array_unique($reqArray));
?>

The output of array before removing duplicate values from a multi-dimensional array in PHP.

Related Post
Array
(
    [name] => adam
    [sub] => english
)

PHP Multidimensional Array

$reqArray = array(
      array(
          "name" => "john"
      ),
      array(
          "age" => "12"
      ),
      array(
          "address" => "Delhi"
      ),
      array(
          "name" => "john"
      )

  );

Remove Duplicate Values from Multidimensional Array:

We will follow the following steps to create a PHP function that will remove duplicate values from the multi dimensional array.
Step 1: First we will use serialize() funtion to serialize the array. Then use the map with PHP inbuilt function.
Step 2: use unserialize() function to make the serialized string into a PHP value.

Function to remove duplicated values

$reqArray=array(array(
          "name" => "john"
      ),
      array(
          "age" => "12"
      ),
      array(
          "address" => "Delhi"
      ),
      array(
          "name" => "john"
      )
     );
  $output =  array_map("unserialize", array_unique(array_map("serialize", $reqArray)));
     
  echo "<pre>";print_r($output);
}

The output of the array after removing duplicate values from a multi-dimensional array in PHP.

Output:

Array
(
    [0] => Array
        (
            [name] => john
        )

    [1] => Array
        (
            [age] => 12
        )

    [2] => Array
        (
            [address] => Delhi
        )

)

Example 2

Remove Duplicate values From Multi Array

$reqArray=array( array("name","age"), 
 array("depart","salary"), 
 array("name","empiId"), 
 array("name","age")
);

$output =  array_map("unserialize", array_unique(array_map("serialize", $reqArray)));

echo "<pre>";print_r($output);

The output of array after removing duplicate values from a multi-dimensional array in PHP.

Output:

Array
(
    [0] => Array
        (
            [0] => name
            [1] => age
        )

    [1] => Array
        (
            [0] => depart
            [1] => salary
        )

    [2] => Array
        (
            [0] => name
            [1] => empiId
        )

)

Find the Duplicate values in array using PHP

<?php
$array=array("john","12","john", "13", "12");
$count_array = array_count_values($array);
echo "<pre>"; 
print_r($count_array);
?>

Output:

Array
(
    [john] => 2
    [12] => 2
    [13] => 1
)

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories