另一个数组php
我有一个这样的数组:另一个数组php,php,arrays,Php,Arrays,我有一个这样的数组: foreach($this->get_contents() as $item) { $ProducteurId = $item['producteurid']; $ProducteurName = $item['producteur']; $ItemQty = $item['qty']; $ItemName = $item['name']; } 我收到了这样的消息: Red Wine - Qty
foreach($this->get_contents() as $item) {
$ProducteurId = $item['producteurid'];
$ProducteurName = $item['producteur'];
$ItemQty = $item['qty'];
$ItemName = $item['name'];
}
我收到了这样的消息:
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
我怎么做?
谢谢,我有点困了 试试这个:
$arr = array();
foreach ($this->get_contents() as $item) {
if (!empty($arr[$item['producteur']])) $arr[$item['producteur']]+=$item['qty'];
else $arr[$item['producteur']] = $item['qty'];
}
您有一个数组,其中包含“producteur”的名称作为键,以及计数值作为值。如果您只对生产者名称和数量感兴趣,可以执行以下操作:
$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";
}
这里似乎有一个类似的答案,但由于我有一个带有测试数据的工作样本,我还是会在这里给出:
这是我能做的最简单的:它将提供一个producteurid=>total的数组。该循环只是将名称分配给变量。您需要更改这些变量的回显/打印位置…您好,谢谢您的回答,您有示例吗?理想情况下,您可以在向我们展示之前在代码中执行此操作。。。查询数据库的地方。它是通过虚拟购物车而不是数据库,我不必进行查询。嘿,它不起作用了…:/对不起,我误解了你的意思。我以为你想要每个生产者的葡萄酒种类的数量。请尝试编辑后的答案。谢谢大家,dcapilla找到了正确的答案:)如果我只想再添加一个信息,我是说$item['producteurminfact'],大家怎么办?谢谢
$aryTotals = array();
foreach($items as $item) {
$aryTotals[$item['producteurid']] += $item['qty'];
}