简体   繁体   English

在 View 上打印嵌套数组 | 代码点火器

[英]Printing a nested array on View | Codeigniter

I'm building a cart using sessions, I'm adding each pizza to an array and passing to the session.我正在使用会话构建一个购物车,我将每个披萨添加到一个数组中并传递给会话。 The array is as follows,数组如下,

  [cart_items] => Array
        (
            [0] => Array
                (
                    [item_id] => 3
                    [item_name] => New York-Style Pizza
                    [price] => 800.00
                    [toppings] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 1
                                    [name] => Mozarella Cheese 
                                    [price] => 150.00
                                )

                            [1] => Array
                                (
                                    [id] => 7
                                    [name] => Chicken Bacon 
                                    [price] => 250.00
                                )

                        )

                    [toppings_price] => 0
                    [quantity] => 1
                    [price_per_item] => 800
                )

        )

I was able to display the item id, pizza name, price, and quantity But cannot figure out to display the topping names user has selected in the Toppings column.How should I do this>我能够显示项目 ID、比萨名称、价格和数量但无法弄清楚显示用户在浇头列中选择的浇头名称。我该怎么做>

View看法

The following code shows the way that I'm getting values from array,以下代码显示了我从数组中获取值的方式,

<?php $list = $this->session->userdata('cart_items');
        if($list==Null) {
            print_r("There are no items in the your cart");
        }else{
            foreach ($list as $item=>$value){

                echo "<li class='list-group-item d-flex justify-content-between lh-condensed'>";
                print_r("<div>".$value['item_id']."</div>");
                print_r("<div>".$value['item_name']."</div>");
                print_r("<div>".$value['price']."</div>");
                $toppings_names=array($value['toppings']);
                foreach ($toppings_names as $name=>$val){
                print_r("<div>");
                print_r($val['name']);
                print_r("</div>");
                }
                print_r("<div>".$value['quantity']."</div>");
                echo "</li>";
            }
        }
        ?>

Output输出

The output that I'm expecting is as follows我期待的输出如下

Item ID |Item Name|Price|Toppings(selected topping names)|Quantity商品 ID |商品名称|价格|Toppings(选定的浇头名称)|数量

The list is not displaying I'm getting the error "Undefined index: name".列表未显示我收到错误“未定义索引:名称”。

You must make a loop over topping since that is also array, so you can check for each item in it.您必须在topping循环,因为它也是数组,因此您可以检查其中的每个项目。

 <?php

$list = $this->session->userdata('cart_items');    //Array
if ($list == null) {
    print_r("There are no items in the your cart");
} else {
    foreach ($list as $item => $value) {
        echo "<li class='list-group-item d-flex justify-content-between lh-condensed'>";
        print_r("<div>".$value['item_id']."</div>");
        print_r("<div>".$value['item_name']."</div>");
        print_r("<div>".$value['price']."</div>");

        // toppings array is empty (nothing selected)
        if ($value['toppings'] == null) {
            print_r("<div>There is no toppings selected</div>");
        } else {
            foreach ($value['toppings'] as $val) {
                // here you can access indexes id, name, price in toppings array
                print_r("<div>");
                print_r($val['name']);
                print_r("</div>");
            }
        }
        
        print_r("<div>".$value['quantity']."</div>");
        echo "</li>";
    }
}

?> ?>

you can do foreach without variable:您可以在没有变量的情况下执行foreach

if (!empty($value['toppings'])) {
    foreach ($value['toppings'] as $val) {
        print_r("<div>");
        print_r($val['name']);
        print_r("</div>");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM