Rahul
Rahul

Reputation: 842

Merge other key values in multidimentional array if particular key values match

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
        )

)

Upvotes: 1

Views: 35

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92894

array_reduce() + arrray_values() solution:

$multi = [
   ["name" => "Michael", "marks" => "25, 27, 34", "course" => "ABC"],
   ["name" => "Kumar", "marks" => "59, 71, 38", "course" => "DEF"],
   ["name" => "Peter", "marks" => "94, 43, 61", "course" => "JKL"],
   ["name" => "Kumar", "marks" => "83, 57, 73", "course" => "GHI"]
];

$result = array_values(array_reduce($multi, function($r, $a){
    $name = $a['name'];
    if (isset($r[$name])){
        $r[$name]['marks'] .= ', ' . $a['marks'];
        $r[$name]['course'] .= ', ' . $a['course'];
    } else {
        $r[$name] = $a;
    }
    return $r;
}, []));

print_r($result);

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

http://php.net/manual/en/function.array-reduce.php

Upvotes: 2

Related Questions