另一个数组PHP [英] Another array php

查看:119
本文介绍了另一个数组PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的一个数组:

    foreach($this->get_contents() as $item) {


       $ProducteurId = $item['producteurid'];
       $ProducteurName = $item['producteur'];
       $ItemQty = $item['qty'];
       $ItemName = $item['name'];

}

我收到了这样的事情:

I received something like this :

Red Wine - Qty : 5 - ID : 14 - ProducteurName : DOMAINE MAISON PERE & FILS 
White Wine - Qty : 15 - ID : 14 - ProducteurName : DOMAINE MAISON PERE & FILS 
Chmpagne - Qty : 5 - ID : 14 - ProducteurName : DOMAINE MAISON PERE & FILS 
Red Whine - Qty : 5 - ID : 14 - ProducteurName : OTHER DOMAINE 
Whine Wine - Qty : 5 - ID : 14 - ProducteurName : OTHER DOMAINE

我想获得:

DOMAINE MAISON PERE & FILS, the total (qty) so 25.
OTHER DOMAINE, the total (qty) so 10.

对于每个不同的ProducteurName,

For each different ProducteurName,

我该怎么办呢?
感谢我有点卡住...

How can i do it ? Thanks i'm kind of stuck...

推荐答案

如果你只是对生产者的名称和数量有兴趣,你可以做这样的事情:

If you are only interested on the producer name and the quantity, you could do something like this:

$producteurs = array();
foreach($this->get_contents()  as $item) {
    if (!isset($producteurs[$item['producteur']]))
        $producteurs[$item['producteur']] = $item['qty'];
    else
        $producteurs[$item['producteur']] += $item['qty'];
}

//echo the results
foreach ($producteurs as $producteur => $qty)
{
    echo "Producer: $producteur - Quantity: $qty\n";
}

这篇关于另一个数组PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆