如何将数组中的所有行组合为一个输入,使用foreach()?

问题描述 投票:0回答:1

我正在尝试将数组中的所有行组合成一行以用于产品订单摘要。所以我现在拥有的

$cartitem = $item['name'] ." qty ". $item['quantity'] ." IDR ". $item['price']. " IDR " . $item_price;

foreach($cartitem as $rows)

{ echo $rows;}

输出(截至目前)=产品名称数量2 IDR 15000 IDR 30000

这是我的目标输出,但我需要显示所有行。上面的输出只返回最后一行。这可能与foreach(),然后implode所有行插入数据库中的单个列?

*更新:所以这是输入数组的源(推车系统)跳过几行

$item_array = array($productByCode["code"]=>array('name'=>$productByCode["name"], 'code'=>$productByCode["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"], 'image'=>$productByCode["image"]));

            if(!empty($_SESSION["cart_item"])){
                if(in_array($productByCode["name"], array_keys($_SESSION["cart_item"]))){
                foreach($_SESSION["cart_item"] as $k => $v){
                    if($productByCode["code"] == $k){
                        if(empty($_SESSION["cart_item"][$k]["quantity"])){
                        $_SESSION["cart_item"][$k]["quantity"] = 0;
                        }
                        $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                        }
                    }
                }  else { 
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$item_array);
                }
                } else {
                    $_SESSION["cart_item"] = $item_array;
// Above is the cart add-product action

这是购物车输出会话

foreach($_SESSION["cart_item"] as $item){
    $item_price = $item["quantity"]*$item["price"];
?>
    <tr>
    <td><img src="<?php echo $item['image'];?>" class="cart-item-image"><?php echo $item["name"]; ?></td>
    <td><?php echo $item['name']; ?></td>
    <td style="text-align:right;"><?php echo $item['quantity']; ?></td>
    <td style="text-align:right;"><?php echo "IDR ".$item['price']; ?></td>
    <td style="text-align:right;"><?php echo "IDR ". number_format($item_price,2); ?></td>
    <td style="text-align-center;"><a href="cart2.php?action=remove&code=<?php echo $item['code'];?>" class="btn-remove">Hapus</a></td>
    </tr>
<?php
    $total_quantity += $item["quantity"];
    $total_price += ($item_price);
    // Count rows input
    echo count($_SESSION["cart_item"]);
}
?>
// Cart Output Session

我试图通过print_r($_SESSION["cart_item"])检查行,并且它在$item_array中显示形成。我需要一个更整洁的输出来存储到数据库中的一列。

php
1个回答
0
投票

让我解释伪逻辑。

拿一个变量$allProducts

循环产品。

implode() ded字符串附加到它。

现在,每次,最新的结果都将附加到$allProducts字符串。

最后回应字符串。

喜欢...

foreach ($cartitem as $rows) {
 $allProducts .= implode(',', $rows);
}
echo $allProducts;

*来自Bernardy Gosal的编辑 所以我尝试使用这种方法,在我看来,我做了一个新的数组,我就这样做了。

$allProducts= [];
foreach($_SESSION["cart_item"] as $key => $value){
    $allProducts[$value['name']]= $value['quantity']."*".$value['price']."  =Rp".$value["quantity"]*$value["price"]."<br>"; 

}
print_r($allProducts);
$imp_pro .= implode('  ', $allProducts);
print_r($imp_pro);

它产生整洁的输出

Array ( [Central Deluxe 120x200] => 1*1400000 =Rp1400000 <br>
[Central Deluxe 180x200] => 1*1850000 =Rp1850000 <br>
[Central Deluxe 200x200 Pillow Top] => 3*2300000 =Rp6900000
)<br>
But when i try to implode this it seems to lose the $key <br>
1*1400000 =Rp1400000 <br>
1*1850000 =Rp1850000 <br>
3*2300000 =Rp6900000
© www.soinside.com 2019 - 2024. All rights reserved.