Phoenix
Phoenix

Reputation: 322

How do I use the multidimensional array in foreach?

I have an array $cart:

array:1 [
  "product" => array:5 [
      "product_id" => array:2 [
      0 => 2
      1 => 6
    ]
    "product_name" => array:2 [
      0 => "HP Core i3 5th Gen - (4 GB/1 TB HDD/DOS) X5Q17PA 15-be005TU Notebook  (15.6 inch, Turbo SIlver, 2.19 kg)"
      1 => "SAMSUNG 55.88cm (22) Full HD LED TV  (UA22F5100AR, 2 x HDMI, 2 x USB)"
    ]
    "product_description" => array:2 [
      0 => "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)"
      1 => "SAMSUNG 55.88cm (22) Full HD LED TV  (UA22F5100AR, 2 x HDMI, 2 x USB)"
    ]
    "product_image" => array:2 [
      0 => "1481116344.jpeg"
      1 => "1481180186.jpeg"
    ]
    "product_price" => array:2 [
      0 => 350
      1 => 200
    ]
  ]
]

My cart page is something like this:

@foreach()
<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>Product Name</strong></h4>
    <h4><small>Product Body</small></h4>
  </div>
  <div class="col-md-6 col-xs-12">
    <div class="col-md-6 text-right">
     <h4><strong>$ 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

The Output look Like enter image description here

How do I spam the multidimentional array values in this block of code. Suppose there are 3 products then there should be 3 rows.

As the product_name and product_description are different array I am not able to create a foreach where the total rows stays 2 but it spam correct name and body for row.

Note: I am coding in Laravel framework that is why foreach syntax seems different.

Thanks!

Upvotes: 2

Views: 170

Answers (3)

haxxxton
haxxxton

Reputation: 6442

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 ?>

Upvotes: 0

Wesley Smith
Wesley Smith

Reputation: 19571

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

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

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

Upvotes: 2

Related Questions