0

I'm trying to reduce/merge an array looking like this

$test = Array
(
    [0] => Array
        (
            [id] => 150023
            [rowtext] => part 1
            [rowpartnumber] => 4360
            [qty] => 2.00
            [min_delper] => 2021-07-28
        )

    [1] => Array
        (
            [id] => 150026
            [rowtext] => part 2
            [rowpartnumber] => 4360
            [qty] => 2.00
            [min_delper] => 2021-07-29
        )

    [2] => Array
        (
            [id] => 150023
            [rowtext] => part 3
            [rowpartnumber] => 4361
            [qty] => 4.00
            [min_delper] => 2021-08-22
        )

    [3] => Array
        (
            [id] => 150023
            [rowtext] => part 4
            [rowpartnumber] => 4362
            [qty] => 2.00
            [min_delper] => 2021-09-22
        )

);

I want to sum the [qty] where [rowpartnumber] are the same, I can achieve this with

$summa_test = array_reduce($test, function ($a, $b) {
  isset($a[$b['rowpartnumber']]) ? $a[$b['rowpartnumber']]['qty'] += $b['qty'] : $a[$b['rowpartnumber']] = $b;  
  return $a;
});

But I would also like to take the [id] where [rowpartnumer] are the same and add it to [id] so the output should look like this:

Array
(
    [0] => Array
        (
            [id] => 150023 150026
            [rowtext] => part 1
            [rowpartnumber] => 4360
            [qty] => 4.00
            [min_delper] => 2021-07-28
        )

    [1] => Array
        (
            [id] => 150023
            [rowtext] => part 3
            [rowpartnumber] => 4361
            [qty] => 4.00
            [min_delper] => 2021-08-22
        )

    [2] => Array
        (
            [id] => 150023
            [rowtext] => part 4
            [rowpartnumber] => 4362
            [qty] => 2.00
            [min_delper] => 2021-09-22
        )

);

Can't seem to get my head around it, so a little help from you experts would be great, thank you

1 Answer 1

1

You have to modify the callback function to concatenate the id to the previous one if the rowpartnumber already exits. Like this:

$summa_test = array_reduce($test, function ($carry, $item) {
  if (isset($carry[$item['rowpartnumber']])) {
      $carry[$item['rowpartnumber']]['qty'] += $item['qty'];
      $carry[$item['rowpartnumber']]['id'] .= " ".$item['id'];
  }
  else {
      $carry[$item['rowpartnumber']] = $item;
  }
  return $carry;
});

I don't know whether it is a problem, but because of the concatenation id will become a string where multiple ids are found - but remains integer when only one. If it is a problem for you, there are two options:

  • Convert to string even if only one id
  • Use an array to store to multiple ids.

(I changed the parameter names to the ones used in PHP documentation - you can change it back, but generally it is a good idea to keep that way - others will more easily understand your code.)

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

1 Comment

Awesome, that is exactly what I was looking for, regarding the id its not going to be a problem if it's a string for me. And thank you for excellent educational explanation

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.