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 illustarte 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
)
)
)
Recommended Posts:
- PHP multidimensional array search by value
- How to search by key=>value in a multidimensional array in PHP ?
- Multidimensional Associative Array in PHP
- How to check an array is multidimensional or not in PHP ?
- Multidimensional array in JavaScript
- Convert multidimensional array to XML file in PHP
- Sort a multidimensional array by date element in PHP
- PHP | Remove duplicate elements from Array
- Multidimensional arrays in PHP
- Perl | Multidimensional Hashes
- Perl | Multidimensional Arrays
- Multidimensional Arrays in Java
- D3.js | d3.merge() function
- ES6 | Merge Objects
- Git-Checkout And Merge
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


