Check duplicate value in multidimensional array php

I have this issue with multidimensional arrays.

Given the following multidimensional array:

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

I want to check its values and find duplicates (i.e. keys 0, 2 and 3) leaving just one key - value pair deleting the others, resulting in somthing like this:

Array(
    [0] => Array("a", "b", "c")
    [1] => Array("x", "y", "z")
    [2] => Array("a", "x", "z")
    )

How can I do that??

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

<?php
$arrays = array(
array(
'name'=>'foo',
),
array(
'name'=>'bar',
),
array(
'name'=>'foo',
),
array(
'name'=>'foo',
'type'=>'baz',
),
);
echo "find completely identical arrays\n";
foreach ($arrays as $current_key => $current_array) {
$search_key = array_search($current_array, $arrays);
echo "current key: $current_key\n";
echo "search key: $search_key\n";
if ($current_key != $search_key) {
echo "duplicate found for item $current_key\n";
}
echo "\n";
}
echo "\n\nfind arrays with duplicate value for 'name'\n";
foreach ($arrays as $current_key => $current_array) {
echo "current key: $current_key\n";
foreach ($arrays as $search_key => $search_array) {
if ($search_array['name'] == $current_array['name']) {
if ($search_key != $current_key) {
echo "duplicate found: $search_key\n";
}
}
}
echo "\n";
}

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    To merge the duplicate value in a multidimensional array in PHP, first, create an empty array that will contain the final result. Then we iterate through each element in the array and check for its duplicity by comparing it with other elements. If duplicity is found then first merge the duplicate elements and then push it to the final array else directly push to the final array.
    Below example illustrate the above approach in a more significant way:
    Example 1: In this example the duplicity is not more than two. 
     

    PHP

    <?php

    $arr = array(

        array('Roll'=>43, 'name'=>'Geeeks', 'subject'=>'Course-011'),

        array('Rool'=>38, 'name'=>'Gfg', 'subject'=>'Course-012'),

        array('Rool'=>43, 'name'=>'Geeks', 'subject'=>'Course-011')

    );

    $myarray = array();

    $keys = array_keys($arr);

    for($i = 0; $i < count($arr); $i++) {

        $keys = array_keys($arr);

        foreach($arr[$keys[$i]] as $key => $value) {

            $f = 0;

            for($j = 0; $j < count($arr); $j++) {

                if($i != $j) {

                    foreach($arr[$keys[$j]] as $key1 => $value1) {

                        if(($key1 == $key) && ($value == $value1)) {

                            $f = 1;

                            $dup_ind = $j;

                        }

                    }

                }

            }

        }

        if($f ==1 ) {

            $temp_arr = array();

            array_push($temp_arr, $arr[$i]);

            array_push($temp_arr, $arr[$dup_ind]);

            unset($arr[$dup_ind]);

            array_push($myarray, $temp_arr);

        }

        else {

            array_push($myarray, $arr[$keys[$i]]);

        }

    }

    print_r($myarray);

    ?>

    Output: 
     

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [Roll] => 43
                        [name] => Geeeks
                        [subject] => Course-011
                    )
    
                [1] => Array
                    (
                        [Rool] => 43
                        [name] => Geeks
                        [subject] => Course-011
                    )
    
            )
    
        [1] => Array
            (
                [Rool] => 38
                [name] => Gfg
                [subject] => Course-012
            )
    
    )

    Example 2: In this example the duplicate field is more than two. 
     

    php

    <?php

    $arr = array(

        array('Roll'=>43, 'name'=>'Geeks', 'subject'=>'Course-011'),

        array('Roll'=>38, 'name'=>'GFG', 'subject'=>'Course-011'),

        array('Roll'=>26, 'name'=>'GeeksforGeeks', 'subject'=>'Course-011'),

        array('Roll'=>31, 'name'=>'gfg', 'subject'=>'Course-012')

    );

    foreach($arr as $k => $v) {

        $new_arr[$v['subject']][]=$v;

    }

    print_r($new_arr);

    ?>

    Output: 
     

    Array
    (
        [Course-011] => Array
            (
                [0] => Array
                    (
                        [Roll] => 43
                        [name] => Geeks
                        [subject] => Course-011
                    )
    
                [1] => Array
                    (
                        [Roll] => 38
                        [name] => GFG
                        [subject] => Course-011
                    )
    
                [2] => Array
                    (
                        [Roll] => 26
                        [name] => GeeksforGeeks
                        [subject] => Course-011
                    )
    
            )
    
        [Course-012] => Array
            (
                [0] => Array
                    (
                        [Roll] => 31
                        [name] => gfg
                        [subject] => Course-012
                    )
    
            )
    
    )

    How can I get same value in multidimensional array in PHP?

    To merge the duplicate value in a multidimensional array in PHP, first, create an empty array that will contain the final result. Then we iterate through each element in the array and check for its duplicity by comparing it with other elements.

    How to remove duplicate values from multidimensional array?

    The array_unique() function are used to remove duplicate data from given array. There are two parameters that will be passed in the array_unique() , first specifying an array that is required and second is optional specifying the sorting type.

    How can I remove duplicate values from a multi dimensional array in PHP?

    Here we will take an example to remove duplicate elements or values from a multidimensional array using PHP functions. ); $arr = array_map("unserialize", array_unique(array_map("serialize", $arr))); print_r($arr);

    How to combine two multidimensional array in PHP?

    The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.