Skip to content
Related Articles

Related Articles

Improve Article
How to merge the duplicate value in multidimensional array in PHP ?
  • Last Updated : 11 Feb, 2021

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')
);
     
 
// Create an empty array
$myarray = array();
     
// It returns the keys of the array
$keys = array_keys($arr);
 
// Iterating through the multidimensional array
for($i = 0; $i < count($arr); $i++) {
 
    $keys = array_keys($arr);
 
    // Iterating through each element in array
    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) {
     
                    // Checking for duplicacy
                    if(($key1 == $key) && ($value == $value1)) {
                        $f = 1;
     
                        // String index where the duplicate
                        // array exists
                        $dup_ind = $j;
                    }
                }
            }
        }
    }
     
    // If duplicate is found
    if($f ==1 ) {
        $temp_arr = array();
        array_push($temp_arr, $arr[$i]);
         
        // Merge both the arrays
        array_push($temp_arr, $arr[$dup_ind]);
         
        // Remove the duplicate element from original array
        unset($arr[$dup_ind]);
         
        // Finally push in the result array
        array_push($myarray, $temp_arr);
    }
    else {
         
        // Directly push in the result array
        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
                )

        )

)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :