PHP remove duplicates from multidimensional array

Remove duplicates elements from an array in PHP. Here 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.

PHP Remove Duplicate Values From Multidimensional Array

When we work with PHP or PHP frameworks like laravel, Codeigniter, Zend, WordPress, etc. So sometimes we need to remove duplicates values/elements/items from a multidimensional array in our PHP. In this example tutorial, we are going to give you an example of getting unique values from a multidimensional array that way duplicates will be deleted. It helps to remove duplicates values from multidimensional array.

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);

?>

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);  
?>

Leave a Reply

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