How do I use the multidimensional array in foreach?

后端 未结 3 1682
庸人自扰
庸人自扰 2021-01-23 02:26

I have an array $cart:

array:1 [
  "product" => array:5 [
      "product_id" => array:2 [
      0 => 2
      1 =>         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-23 03:15

    I would reformat the input like this but what you need is @foreach($products as $product) $products being whatever your current variable is.

    If you dont want to change the format of the input (really, I recommend it) use Alexey Mezenin's answer

    $products = [
        ['id' => '2',
            'name' => 'HP Core i3 5th Gen - (4 GB/1 TB HDD/DOS) X5Q17PA 15-be005TU Notebook  (15.6 inch, Turbo SIlver, 2.19 kg)',
            'description' => 'SAMSUNG 55.88cm (22) Full HD LED TV  (UA22F5100AR, 2 x HDMI, 2 x USB)',
            'image' => '1481116344.jpeg',
            'price' => '350',
        ],
        ['id' => '6',
            'name' => 'HP Core i3 5th Gen - (4 GB/1 TB HDD/DOS) X5Q17PA 15-be005TU Notebook  (15.6 inch, Turbo SIlver, 2.19 kg)HP Core i3 5th Gen - (4 GB/1 TB HDD/DOS) X5Q17PA 15-be005TU Notebook  (15.6 inch, Turbo SIlver, 2.19 kg)',
            'description' => 'SAMSUNG 55.88cm (22) Full HD LED TV  (UA22F5100AR, 2 x HDMI, 2 x USB)',
            'image' => '1481116344.jpeg',
            'price' => '200',
        ]
    ];
    
    
    
    @foreach($products as $product)
        

    {{$product['name']}}

    {{$product['description']}}

    ${{$product['price']}} x


    @endforeach

提交回复
热议问题