2

I have an array from a database that I'd like to loop through and where the item_id is the same, add the qty(quantity) to get a total quantity. I've tried using a while loop to into the inner arrays with a maximum count determined by count(outer array) and this hasn't presented me with a desired outcome.

Here's the array:

Array
(
    [0] => Array
        (
            [id] => 8
            [lpo_id] => 20
            [invoice_no] => 1483958702
            [external_invoice_no] => 1
            [item_id] => 21
            [qty] => 14
            [currency] => USD
            [price] => 1.31
            [date] => 1483909200
            [supplier_id] => 9
        )

    [1] => Array
        (
            [id] => 9
            [lpo_id] => 20
            [invoice_no] => 1483958702
            [external_invoice_no] => 1
            [item_id] => 22
            [qty] => 15
            [currency] => USD
            [price] => 2.52
            [date] => 1483909200
            [supplier_id] => 9
        )

)

Here's the DB query:

public function getSupplierInvoiceByRefNo($ourRef) {
  $sql = "SELECT * FROM `{$this->_table_20}` WHERE `invoice_no` = '$ourRef'";
  return $this->db->fetchAll($sql);
}

public function fetchAll($sql) {
    $result = $this->query($sql);
    $out = array();
    while($row = mysqli_fetch_assoc($result)) {
        $out[] = $row;
    }
    mysqli_free_result($result);
    return $out;
}

DB Table:

id | lpo_id | invoice_no | external_invoice_no | item_id | qty | currency | price | date | supplier_id

Desired output is to have the following table where line items with same item_id have a totaled qty and are displayed only once in the output table

item_id | qty | price | currency
11
  • Unable to get your question what is your desired output? Commented Jan 9, 2017 at 11:20
  • Sorry, but I fail to see your php code you said you tried. How do you expect us to help with it if you do not post it? Commented Jan 9, 2017 at 11:20
  • And why should list() not be available in php versions lower than 7? Commented Jan 9, 2017 at 11:21
  • I would say you could do this quicker and more simply by changing the query you run on the database to do this all for you. Post your query. Always try to fix the source of the error, Fiddling rubbish into good data is a bad practice Commented Jan 9, 2017 at 11:21
  • $final_sum = array(); foreach ($array as $arr){ $final_sum[$arr['item_id']] = $final_sum[$arr['item_id']]+$arr['qty']]; } echo "<pre/>";print_r($final_sum); Commented Jan 9, 2017 at 11:22

4 Answers 4

3

Try the following code:

foreach($arrays as $a) 
{
   foreach ($a as $key => $value) 
       echo $key . '=' . $value;
}

Good luck.

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

1 Comment

Would be simpler to FIX the query to do the selection correctly
2

This query should produce what you are after

SELECT item_id, SUM(qty) as qty, price, currency 
FROM `{$this->_table_20}` 
WHERE `invoice_no` = '$ourRef'
GROUP BY item_id";

Comments

2

You can try something like this

$data = array(
array('id'=>1,'item_id'=>2,'qty'=>13),
array('id'=>2,'item_id'=>3,'qty'=>16),
array('id'=>3,'item_id'=>2,'qty'=>33),
array('id'=>3,'item_id'=>3,'qty'=>43),
array('id'=>3,'item_id'=>4,'qty'=>30));

$new = array();
foreach($data as $value){
if($new[$value['item_id']]){
    $new[$value['item_id']]+= $value[qty];
}else{
  $new[$value['item_id']] = $value['qty'];
}

}echo "<pre>";print_r($new);

Output

Array
(
    [2] => 46
    [3] => 59
    [4] => 30
)

1 Comment

Would be simpler to FIX the query to do the selection correctly
2

I think you want like this:-

$final_sum = array();

foreach ($array as $arr){
  $final_sum[$arr['item_id']] += $arr['qty']];
}

echo "<pre/>";print_r($final_sum); // will give you an array of keys = item_id and values = sum of those id's quantities from original array

Note:- It will be really easy if you do it through query by using SUM() as suggested by @RiggsFolly

3 Comments

Would be simpler to FIX the query to do the selection correctly
@RiggsFolly aggreed
I would also prefer a query alternative if possible.

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.