How to merge the duplicate 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. 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 illustrates the above approach in a more significant way:
Example 1: In this example, the duplicity is not more than two.
<?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
$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
)
)
)
Using Associative array keys
This PHP method iterates through a multidimensional array, generating unique keys based on concatenated values of specified fields (‘Roll’, ‘name’, ‘subject’). It merges or adds elements based on these keys, ensuring uniqueness without relying on complex nested structures.
Example: This example shows the implementation of the above-mentioned approach.
<?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')
);
$uniqueArray = [];
foreach ($arr as $item) {
// Use a unique key based on concatenated values
$key = $item['Roll'] . '-' . $item['name'] . '-' . $item['subject'];
// If key exists, merge values; otherwise, add new entry
if (isset($uniqueArray[$key])) {
$uniqueArray[$key] = array_merge($uniqueArray[$key], $item);
} else {
$uniqueArray[$key] = $item;
}
}
// Convert associative array back to indexed array
$uniqueArray = array_values($uniqueArray);
print_r($uniqueArray);
?>
Output
Array ( [0] => Array ( [Roll] => 43 [name] => Geeeks [subject] => Course-011 ) [1] => Array ( [Rool] => 38 ...
Using array_reduce() with a Callback Function:
This method involves using array_reduce() to iterate through the array, merging elements with the same key based on specified conditions. It efficiently consolidates duplicates into a single array structure.
Example
<?php
$array = [
['Roll' => 43, 'name' => 'Geeks', 'subject' => 'Course-011'],
['Roll' => 43, 'name' => 'Geeks', 'subject' => 'Course-011'],
['Roll' => 38, 'name' => 'Gfg', 'subject' => 'Course-012'],
['Roll' => 31, 'name' => 'gfg', 'subject' => 'Course-012'],
['Roll' => 26, 'name' => 'GeeksforGeeks', 'subject' => 'Course-011']
];
$mergedArray = array_reduce($array, function($result, $item) {
$key = $item['subject'];
if (!isset($result[$key])) {
$result[$key] = [];
}
$result[$key][] = $item;
return $result;
}, []);
print_r($mergedArray);
?>
Output
Array ( [Course-011] => Array ( [0] => Array ( [Roll] => 43 [name] => Geeks [subject] => Course-011 ...