-3

this is quite beyond me. Appreciate some help. I have an array in php like so:

[0] => Array
    (
        [cust_id] => 1006
        [no_of_subs] => 2
        [dlv_id] => 1000
    )

[1] => Array
    (
        [cust_id] => 1011
        [no_of_subs] => 3
        [dlv_id] => 1000
    )

[2] => Array
    (
        [cust_id] => 1012
        [no_of_subs] => 5
        [dlv_id] => 1001
    )

[3] => Array
    (
        [cust_id] => 1013
        [no_of_subs] => 6
        [dlv_id] => 1001
    )

I don't need the cust_id field. I just need to group the dlv_id and the sum of no_of_subs for each matching dlv_id. The result should look like this:

[0] => Array
    (
        [dlv_id] => 1000
        [no_of_subs] => 5

    )

[1] => Array
    (
        [cust_id] => 1011
        [no_of_subs] => 11

    )

Thank you for any help.

I don't understand the downvotes for this question. Am i doing it all wrong? Downvoting without a reason is not helping.

schenker
  • 941
  • 3
  • 12
  • 25
  • 1
    Use `foreach` and count values. – u_mulder Sep 24 '18 at 14:34
  • @schenker I suspect the downvotes are a product of you not including your coding attempt. Normally, I don't answer questions without a coding attempt, but I didn't like the upvoted answer, so I posted a superior approach so that future researchers didn't copy an indirect process. If you did attempt to self-solve, please update your question. – mickmackusa Sep 24 '18 at 18:20
  • Thank you. The downvotes bothered me if im getting it all wrong. I didn't post my attempt as it was producing a totally different result which i thought would be of no use to post here. Noted your advice. Thanks again. – schenker Sep 25 '18 at 00:19
  • It is not too late to edit your question. We actually appreciate seeing your broken/failing code because in addition to providing our best suggested snippet, we can clear up any misconceptions you might have with your code. I urge you to post it -- even if it is super-not-good. – mickmackusa Sep 25 '18 at 00:36

2 Answers2

2

The simplest, most efficient way to group and sum is to perform a single loop and assign temporary associative keys.

When a row is identified as a new dlv_id row, save the two desired elements, otherwise add the no_of_subs value to the pre-existing value.

Optionally, remove the temporary keys with array_values().

Code (Demo)

$array = [
    ["cust_id" => 1006, "no_of_subs" => 2, "dlv_id" => 1000],
    ["cust_id" => 1011, "no_of_subs" => 3, "dlv_id" => 1000],
    ["cust_id" => 1012, "no_of_subs" => 5, "dlv_id" => 1001],
    ["cust_id" => 1013, "no_of_subs" => 6, "dlv_id" => 1001]
];

foreach ($array as $row) {
    if (!isset($result[$row["dlv_id"]])) {
        $result[$row["dlv_id"]] = ["dlv_id" => $row["dlv_id"], "no_of_subs" => $row["no_of_subs"]];
    } else {
        $result[$row["dlv_id"]]["no_of_subs"] += $row["no_of_subs"];
    }
}
var_export(array_values($result));

Output:

array (
  0 => 
  array (
    'dlv_id' => 1000,
    'no_of_subs' => 5,
  ),
  1 => 
  array (
    'dlv_id' => 1001,
    'no_of_subs' => 11,
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0
  • Using array_column function, we can extract out dlv_id and no_of_subs separately in two different arrays, using cust_id as the key.
  • Now, simply loop over the array of dlv_id, and if matching key found, add the no_of_subs to it, else set the value (for the first time).
  • We use isset function to check if the key exists already or not.

Try the following:

// your input array is $input_array

// get all dlv_id maintaining the cust_id as index
$dlv_id = array_column($input_array, 'dlv_id', 'cust_id');

// get all no_of_subs maintaining the cust_id as index
$no_of_subs = array_column($input_array, 'no_of_subs', 'cust_id');

$output = array();

foreach ($dlv_id as $key => $value) {

    if (isset($output[$value]['dlv_id'])) {
        $output[$value]['dlv_id'] += $no_of_subs[$key];
    } else {
        $output[$value]['dlv_id'] += $no_of_subs[$key];
    }
}
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
  • What is your thinking with that condition block? See something odd there? I hope my answer will inspire you in the future; your solution is iterating the input array 2 times, then iterating 1st manufactured array to perform the addition, and this is not efficient. Not only is the solution inefficient, it generates Notices, and it doesn't generate the OP's desired output. I have downvoted this answer to discourage future researchers from using it. Demo: https://3v4l.org/irHJT – mickmackusa Sep 25 '18 at 00:33