How do I use the multidimensional array in foreach?

后端 未结 3 1683
庸人自扰
庸人自扰 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)
        <div class="row">
            <div class="col-md-2 col-xs-12">
                <img class="img-responsive" src="{{asset($product['image'])}}">
            </div>
            <div class="col-md-4 col-xs-12">
                <h4><strong>{{$product['name']}}</strong></h4>
                <h4><small>{{$product['description']}}</small></h4>
            </div>
            <div class="col-md-6 col-xs-12">
                <div class="col-md-6 text-right">
                    <h4><strong>${{$product['price']}}</strong> x</h4>
                </div>
                <div class="col-md-4 col-xs-9">
                    <input type="text" class="form-control input-sm" placeholder="quantity">
                </div>
                <div class="col-md-2 col-xs-2">
                    <button class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span></button>
                </div>
            </div>
        </div>
        <hr>
    @endforeach
    
    0 讨论(0)
  • 2021-01-23 03:19

    Use @for() to iterate over array:

    @for ($i = 0; $i < count($cart['product']['product_id']); $i++)
        <div class="row">
           <div class="col-md-2 col-xs-12">
               <img class="img-responsive" src="{{ asset('images/150x70.png') }}">
           </div>
           <div class="col-md-4 col-xs-12">
               <h4><strong>{{ $card['product']['product_name'][$i] }}</strong></h4>
               <h4><small>{{ $card['product']['product_description'][$i] }}</small></h4>
           </div>
           <div class="col-md-6 col-xs-12">
               <div class="col-md-6 text-right">
               <h4><strong>$ {{ $card['product']['product_price'][$i] }}</strong> x</h4>
               </div>
               <div class="col-md-4 col-xs-9">
                   <input type="text" class="form-control input-sm" placeholder="quantity">
                </div>
                <div class="col-md-2 col-xs-2">
                <button class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span></button>
                </div>
            </div>
        </div>
        <hr>    
    @endfor
    
    0 讨论(0)
  • 2021-01-23 03:29

    if product_id is a consistent field for your product array, you can use its length as an iterator count to perform lookups on the other keys. WARNING this assumes that ALL keys you're using to output into the template have the same length as product_id, and that each index within each key refers to the same product.

    You dont appear to have a key for quantity so havent populated that field

    <?php for ($i = 0; $i <= count($cart['product']['product_id']); $i++): ?>
        <div class="row">
          <div class="col-md-2 col-xs-12">
            <img class="img-responsive" src="<?php echo $cart['product']['product_image'][$i]; ?>">
          </div>
          <div class="col-md-4 col-xs-12">
            <h4><strong><?php echo $cart['product']['product_name'][$i]; ?></strong></h4>
            <h4><small><?php echo $cart['product']['product_description'][$i]; ?></small></h4>
          </div>
          <div class="col-md-6 col-xs-12">
            <div class="col-md-6 text-right">
             <h4><strong>$ <?php echo $cart['product']['product_price'][$i]; ?></strong> x</h4>
            </div>
            <div class="col-md-4 col-xs-9">
             <input type="text" class="form-control input-sm" placeholder="quantity">
            </div>
            <div class="col-md-2 col-xs-2">
             <button class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span></button>
            </div>
          </div>
        </div>
        <hr>    
    <?php endfor ?>
    

    PS. unfortunately i am unfamiliar with Laravel syntax, so this is in standard PHP


    EDIT

    Because it didnt sit right with me to be using that format i have created a converter function that should turn your array into something more manageable

    <?php 
    
    function convertCartArray($cart){
        // create new container array
        $products = array();
        // create array of all the keys in the old cart that you wish to convert
        $keys = array('product_id', 'product_name', 'product_description', 'product_image', 'product_price');
        // for each product
        for ($i = 0; $i < count($cart['product']['product_id']); $i++){
            // create a new product array
            $product = array();
            // for each of the keys
            for ($j = 0; $j < count($keys); $j++){
                // convert old cart product detail over to product detail
                $product[$keys[$j]] = $cart['product']$keys[$j][$i];
            }
            // add product to productsArray
            array_push($products, $product);
        }
        // return new products array
        return $products;
    }
    
    $productsArray = convertCartArray($cart);
    
    for ($i = 0; $i <= count($productsArray); $i++): ?>
        <div class="row">
          <div class="col-md-2 col-xs-12">
            <img class="img-responsive" src="<?php echo $productsArray[$i]['product_image']; ?>">
          </div>
          <div class="col-md-4 col-xs-12">
            <h4><strong><?php echo $productsArray[$i]['product_name']; ?></strong></h4>
            <h4><small><?php echo $productsArray[$i]['product_description']; ?></small></h4>
          </div>
          <div class="col-md-6 col-xs-12">
            <div class="col-md-6 text-right">
             <h4><strong>$ <?php echo $productsArray[$i]['product_price']; ?></strong> x</h4>
            </div>
            <div class="col-md-4 col-xs-9">
             <input type="text" class="form-control input-sm" placeholder="quantity">
            </div>
            <div class="col-md-2 col-xs-2">
             <button class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span></button>
            </div>
          </div>
        </div>
        <hr>    
    <?php endfor ?>
    
    0 讨论(0)
提交回复
热议问题