I have a multidimensional array, in which I have a few set of values. What I want to do here is to merge the key values "marks" and "course" if the values of the key "name" match. So far, I've done something like below to remove the duplicates:
$multi = array(
array("name" => "Michael", "marks" => "25, 27, 34", "course" => "ABC"),
array("name" => "Kumar", "marks" => "59, 71, 38", "course" => "DEF"),
array("name" => "Peter", "marks" => "94, 43, 61", "course" => "JKL"),
array("name" => "Kumar", "marks" => "83, 57, 73", "course" => "GHI"),
);
$multiTemp = $multiNew = array();
foreach($multi as $key=>$val){
if(array_key_exists($val['name'], $multiTemp) ) {
continue;
}
$multiTemp[$val['name']] = 1;
$multiNew[] = $val;
}
echo "<pre>";
print_r($multiNew);
echo "</pre>";
It just removes the duplicate values. Is therey any way to merge the other two values based on the condition I mentioned above the code? Just like the second and fourth array in the array $multi carry same values for name, so I want marks and course to be mereged into one. Thanks in advance for your help.
Current Output:
Array
(
[0] => Array
(
[name] => Michael
[marks] => 25, 27, 34
[course] => ABC
)
[1] => Array
(
[name] => Kumar
[marks] => 59, 71, 38
[course] => DEF
)
[2] => Array
(
[name] => Peter
[marks] => 94, 43, 61
[course] => JKL
)
)
Expected Output:
Array
(
[0] => Array
(
[name] => Michael
[marks] => 25, 27, 34
[course] => ABC
)
[1] => Array
(
[name] => Kumar
[marks] => 59, 71, 38, 83, 57, 73
[course] => DEF, GHI
)
[2] => Array
(
[name] => Peter
[marks] => 94, 43, 61
[course] => JKL
)
)