简体   繁体   中英

How can I loop through this array?

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

Try the following code:

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

Good luck.

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

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
)

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM