简体   繁体   English

我怎样才能遍历这个数组?

[英]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. 我有一个数据库中的数组,我想循环,并且item_id是相同的,添加数量(数量)以获得总数量。 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. 我已经尝试使用while循环进入内部数组,最大计数由count(外部数组)确定,这并没有给我一个期望的结果。

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的订单项具有总计数量并且在输出表中仅显示一次

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 注意: - 如果你通过使用@RiggsFolly建议的SUM()进行查询,那将非常容易

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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