0

I have long arrays (could be hundreds) from $_POST and need to summarize the qty.

Below is the $_POST result:

array(5) {
  ["Batch_No"]=>
  array(3) {
    [0]=>
    string(7) "AAAV343"
    [1]=>
    string(7) "AAAV343"
    [2]=>
    string(7) "AAAV347"
  }
  ["Expire"]=>
  array(3) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
  }
  ["Prod_ID"]=>
  array(3) {
    [0]=>
    string(5) "00041"
    [1]=>
    string(5) "00041"
    [2]=>
    string(5) "00041"
  }
  ["zID_Line"]=>
  array(3) {
    [0]=>
    string(2) "17"
    [1]=>
    string(2) "17"
    [2]=>
    string(2) "17"
  }
  ["Qty"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "1"
    [2]=>
    string(1) "1"
  }
}

I need to summarize the array using Batch_No and Prod_ID, so that the result would be become like this:

array(5) {
  ["Batch_No"]=>
  array(2) {
    [0]=>
    string(7) "AAAV343"
    [1]=>
    string(7) "AAAV347"
  }
  ["Expire"]=>
  array(2) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
  }
  ["Prod_ID"]=>
  array(2) {
    [0]=>
    string(5) "00041"
    [1]=>
    string(5) "00041"
  }
  ["zID_Line"]=>
  array(2) {
    [0]=>
    string(2) "17"
    [1]=>
    string(2) "17"
  }
  ["Qty"]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "1"
  }
}

The Qty is basically the TOTAL of each array which has identical Batch_No AND Prod_ID.

Been trying to explore existing topics, but cannot resolve the issues due to those two keys.

3
  • 2
    Show the code that you've been trying. Commented Mar 9, 2015 at 10:25
  • You're struggling with this because you have a datastructure issue, not really a code one, though you may be in a situation where there's little you can do about that. You'll probably save time, and definitely make things easier on future devs, if your form is nicely formatted so batches are unique and contain their own properties. This is one case where a few minutes of client-side work (e.g. submitting this as JSON data, or better-organizing the form inputs so batches contain their properties) will save you hours of doing things the wrong-way server side. Commented Mar 9, 2015 at 13:45
  • My goal was actually to avoid traveling to database, but I guess this is a bit push the limit. I agree there are other ways of doing this correctly. And yes, it is much easier doing this grouping by using the database. Simply GROUP BY Prod_ID, Batch_No and SUM(Qty_SPTB) AS Qty_Total, issue solved! Thanks everyone. Commented Mar 9, 2015 at 22:31

1 Answer 1

1

It is not totally clear to me what you mean by "summarize the array using Batch_No and Prod_ID": I see no 'uniqueness' in the Prod_ID. Hoewever, the following code will create an array ($totals) with only unique 'Batch_No''s and for those, all (first) matching values of Expire, Prod_ID and zID_Line, and the sum of the Qty's.

$totals = array(
    'Batch_No' => array(),
    'Expire' => array(),
    'Prod_ID' => array(),
    'zID_Line' => array(),
    'Qty' => array(),
);
foreach($_POST['Batch_No'] as $key => $value) {
    if ($key2 = array_search($value, $totals['Batch_No'])) {
        $totals['Qty'][$key2] += $_POST['Qty'][$key];
    } else {
        $totals['Batch_No'][] = $_POST['Batch_No'][$key];
        $totals['Expire'][]   = $_POST['Expire'][$key];
        $totals['Prod_ID'][]  = $_POST['Prod_ID'][$key];
        $totals['zID_Line'][] = $_POST['zID_Line'][$key];
        $totals['Qty'][]      = $_POST['Qty'][$key];
    }
}

It works by iterating over the POST values. For every value found in Batch_No, it tries to find it in $totals. If it is not there, add it. If it is there already, leave most values alone, but add the quantity found in Qty to the quantity already stored.

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

1 Comment

Hi @marten-koetsier, you are right, my sample arrays don't show the uniqueness of Prod_ID, but in reality there will be hundreds of Prod_ID with vary Batch_No to group with. Your code works well as long as for Batch_No key. Thanks. Anyhow, after over-a-night of sleep (I guess that's all I need), I realize that this is a bit push the limit. I agree with @josh-from-qaribou, there are other ways of doing this correctly. And yes, it is much easier doing this grouping by using the database. Simply GROUP BY Prod_ID, Batch_No and SUM(Qty_SPTB) AS Qty_Total, issue solved! Thanks everyone.

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.