Php Laravel中按名称排列的数组组

Php Laravel中按名称排列的数组组,php,arrays,laravel,Php,Arrays,Laravel,我有一个XML订单列表,我需要按名称列出产品和数量组,以显示如下列表: <ul> <li>Product name A - 3</li> <li>Product name B - 6</li> <li>Product name C- 3/<li> </ul> 阵列是 Array ( [0] => Array (

我有一个XML订单列表,我需要按名称列出产品和数量组,以显示如下列表:

     <ul>
     <li>Product name A - 3</li>
     <li>Product name B - 6</li>
     <li>Product name C- 3/<li>
     </ul>
阵列是

Array
(
    [0] => Array
        (
            [name] => Kong - Size : M (39-42)
            [quantity] => 1
        )

    [1] => Array
        (
            [name] => Kong - Size : M (39-42)
            [quantity] => 1
        )

    [2] => Array
        (
            [name] => Invaders - Size : M (39-42)
            [quantity] => 1
        )

    [3] => Array
        (
            [name] => Super Mamá - Size : S (35-38)
            [quantity] => 1
        )

    [4] => Array
        (
            [name] => Vanlife - Size : M (39-42)
            [quantity] => 1
        )
我需要在正面显示每个产品的数量(按名称排列)

我正在尝试使用一个集合

$namelist = collect($list)->groupBy('name');

但是我不确定什么是最好的方法,我被卡住了。一个选择是循环检查所有项目,并对相同名称的数量求和

如果不需要中间的
$list
,也可以直接对foreach循环中的数量求和

比如说

$results = [];

foreach ($list as $item) {
    array_key_exists($item["name"], $results) ?
        $results[$item["name"]]["quantity"] += $item["quantity"] :
        $results[$item["name"]] = $item;
}

echo "<ul>" . PHP_EOL;;
foreach ($results as $result) {
    echo sprintf("<li>%s - %s</li>", $result["name"] , $result["quantity"]) . PHP_EOL;
}
echo "</ul>";
$results=[];
foreach($项目列表){
数组\键\是否存在($item[“name”],$results)?
$results[$item[“name”][“quantity”]+=$item[“quantity”]:
$results[$item[“name”]=$item;
}
回声“
    ”。PHP_EOL;; foreach($results作为$result){ echo sprintf(“
  • %s-%s
  • ”、$result[“name”]、$result[“quantity”]); } 回声“
”;
输出

<ul>
<li>Kong - Size : M (39-42) - 2</li>
<li>Invaders - Size : M (39-42) - 1</li>
<li>Super Mamá - Size : S (35-38) - 1</li>
<li>Vanlife - Size : M (39-42) - 1</li>
</ul>
  • 孔-尺寸:M(39-42)-2
  • 入侵者-大小:米(39-42)-1
  • 超级马马-尺码:S(35-38)-1
  • Vanlife-尺寸:M(39-42)-1

一个选项是循环所有项目,并对相同名称的数量求和

如果不需要中间的
$list
,也可以直接对foreach循环中的数量求和

比如说

$results = [];

foreach ($list as $item) {
    array_key_exists($item["name"], $results) ?
        $results[$item["name"]]["quantity"] += $item["quantity"] :
        $results[$item["name"]] = $item;
}

echo "<ul>" . PHP_EOL;;
foreach ($results as $result) {
    echo sprintf("<li>%s - %s</li>", $result["name"] , $result["quantity"]) . PHP_EOL;
}
echo "</ul>";
$results=[];
foreach($项目列表){
数组\键\是否存在($item[“name”],$results)?
$results[$item[“name”][“quantity”]+=$item[“quantity”]:
$results[$item[“name”]=$item;
}
回声“
    ”。PHP_EOL;; foreach($results作为$result){ echo sprintf(“
  • %s-%s
  • ”、$result[“name”]、$result[“quantity”]); } 回声“
”;
输出

<ul>
<li>Kong - Size : M (39-42) - 2</li>
<li>Invaders - Size : M (39-42) - 1</li>
<li>Super Mamá - Size : S (35-38) - 1</li>
<li>Vanlife - Size : M (39-42) - 1</li>
</ul>
  • 孔-尺寸:M(39-42)-2
  • 入侵者-大小:米(39-42)-1
  • 超级马马-尺码:S(35-38)-1
  • Vanlife-尺寸:M(39-42)-1