1

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
        )

)
3
  • post the final expected result Commented Feb 6, 2018 at 17:23
  • Hello there, RomanPerekhrest. Here it is: 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 ) ) Commented Feb 6, 2018 at 17:27
  • Edited the question as well, to add the expected output Commented Feb 6, 2018 at 17:28

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I needed, thanks so much Roman! I really appreciate your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.