如何从阵列中设置多个字符串彼此相邻

时间:2017-10-12 16:59:47

标签: php arrays json

我概述了我想要在这样的表格中彼此相邻的产品:" product1,$ 1,35; product2,$ 1,50; product3,$ 5,50"

但现在当我打印出来时,他们会在我的屏幕上看到这样的内容:

enter image description here

这是我的json解码数组的var_dump

array(4) { ["product_id"]=> int(1) ["product_name"]=> string(12) "Broodje Kaas" ["product_quantity"]=> int(1) ["product_price"]=> int(205) } array(4) { ["product_id"]=> int(2) ["product_name"]=> string(11) "Broodje ham" ["product_quantity"]=> int(3) ["product_price"]=> int(107) } array(4) { ["product_id"]=> int(1) ["product_name"]=> string(10) "Broodje ei" ["product_quanity"]=> int(1) ["product_price"]=> int(205) } array(4) { ["product_id"]=> int(2) ["product_name"]=> string(11) "Broodje kip" ["product_quanity"]=> int(3) ["product_price"]=> int(107) } array(4) { ["product_id"]=> int(1) ["product_name"]=> string(12) "Broodje Kaas" ["product_quanity"]=> int(1) ["product_price"]=> int(205) } array(4) { ["product_id"]=> int(2) ["product_name"]=> string(11) "Broodje ham" ["product_quanity"]=> int(3) ["product_price"]=> int(107) }

和我的$ productids的var_dump

array ( [0] => Array ( [id] => 1 [order_items] => [{"product_id":1,"product_name":"Broodje Kaas","product_quantity":1,"product_price":205},{"product_id":2,"product_name":"Broodje ham","product_quantity":3,"product_price":107}] [order_price] => 2,50 [order_date] => 2017-10-12 18:08:43 [order_state] => 1 [order_is_paid] => 1 ) [1] => Array ( [id] => 2 [order_items] => [{"product_id":1,"product_name":"Broodje ei","product_quanity":1,"product_price":205},{"product_id":2,"product_name":"Broodje kip","product_quanity":3,"product_price":107}] [order_price] => 3,00 [order_date] => 2017-10-12 18:04:38 [order_state] => 0 [order_is_paid] => 0 ) [2] => Array ( [id] => 3 [order_items] => [{"product_id":1,"product_name":"Broodje Kaas","product_quanity":1,"product_price":205},{"product_id":2,"product_name":"Broodje ham","product_quanity":3,"product_price":107}] [order_price] => 5,60 [order_date] => 2017-10-12 14:40:51 [order_state] => 2 [order_is_paid] => 1 ) ) Array ( [0] => Array ( [id] => 1 [order_items] => [{"product_id":1,"product_name":"Broodje Kaas","product_quantity":1,"product_price":205},{"product_id":2,"product_name":"Broodje ham","product_quantity":3,"product_price":107}] [order_price] => 2,50 [order_date] => 2017-10-12 18:08:43 [order_state] => 1 [order_is_paid] => 1 ) [1] => Array ( [id] => 2 [order_items] => [{"product_id":1,"product_name":"Broodje ei","product_quanity":1,"product_price":205},{"product_id":2,"product_name":"Broodje kip","product_quanity":3,"product_price":107}] [order_price] => 3,00 [order_date] => 2017-10-12 18:04:38 [order_state] => 0 [order_is_paid] => 0 ) [2] => Array ( [id] => 3 [order_items] => [{"product_id":1,"product_name":"Broodje Kaas","product_quanity":1,"product_price":205},{"product_id":2,"product_name":"Broodje ham","product_quanity":3,"product_price":107}] [order_price] => 5,60 [order_date] => 2017-10-12 14:40:51 [order_state] => 2 [order_is_paid] => 1 ) ) Array ( [0] => Array ( [id] => 1 [order_items] => [{"product_id":1,"product_name":"Broodje Kaas","product_quantity":1,"product_price":205},{"product_id":2,"product_name":"Broodje ham","product_quantity":3,"product_price":107}] [order_price] => 2,50 [order_date] => 2017-10-12 18:08:43 [order_state] => 1 [order_is_paid] => 1 ) [1] => Array ( [id] => 2 [order_items] => [{"product_id":1,"product_name":"Broodje ei","product_quanity":1,"product_price":205},{"product_id":2,"product_name":"Broodje kip","product_quanity":3,"product_price":107}] [order_price] => 3,00 [order_date] => 2017-10-12 18:04:38 [order_state] => 0 [order_is_paid] => 0 ) [2] => Array ( [id] => 3 [order_items] => [{"product_id":1,"product_name":"Broodje Kaas","product_quanity":1,"product_price":205},{"product_id":2,"product_name":"Broodje ham","product_quanity":3,"product_price":107}] [order_price] => 5,60 [order_date] => 2017-10-12 14:40:51 [order_state] => 2 [order_is_paid] => 1 ) )

这是我的代码

<table class="table table-striped">
<?php $output = "<tr><th># </th><th>Producten </th><th>Prijs </th></tr>";?>
<tbody>
<tr>

Uw bestelde producten

<?php foreach ($productsids as $value) {

    $output .= "<tr>";
    $orderitems = json_decode($value['order_items'], true);
    $output .= "<td>" .$value['id']. "</td>";

    foreach ($orderitems as $item) {
        var_dump($item);

        $output .= "<td>" .$item['product_name']. "</td>";
        $output .= "<td>" .$item['product_price']. "</td>";

        //$output .= "<td>" .$item['product_quantity']. "</td>";

    }
    $output .= "</tr>";

}
echo $output;
?>

</tr>
</tbody>

1 个答案:

答案 0 :(得分:1)

如果您每行放置一个产品,则需要在子产品的foreach中处理tr,并将其与第一个包含行号的单元格进行跨越。

这样的事情:

foreach ($productsids as $value) {
    $orderitems = json_decode($value['order_items'], true);
    $output .= "<tr>";
    $output .= '<td rowspan="'. count($orderitems) .'">' . $value['id'] . "</td>";
    foreach ($orderitems as $r => $item) {
        if ($r!=0) { $output .= "</tr><tr>"; }
        $output .= "<td>" . $item['product_name'] . "</td>";
        $output .= "<td>" . $item['product_price'] . "</td>";
    }
    $output .= "</tr>";
}