skip to Main Content

I’ve array like this in php I want to loop this array inside div elements in php:

`Array
(
    [item] => Array
        (
            [0] => Masaladosa
            [1] => Zingafry
            [2] => Idli
            [3] => Tandoori Baby Corn Mushroom
            [4] => Bharwan Aloo
        )
    [qty] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 2
            [3] => 2
            [4] => 2
        )
    [price] => Array
        (
            [0] => 320
            [1] => 800
            [2] => 70
            [3] => 560
            [4] => 560
       )
)`

I want to use loop this array inside following div:

<div class="row mt-4 mx-1">
    <div class="col-6 text-start item-listmenu">
    <p class="bill-order">Masala dosa</p>
    </div>
    <div class="col-2 text-start">
        <p class="bill-order">2</p>
        </div>
    <div class="col-4 text-end">
    <p class="bill-order"><span>₹</span>320</p>
    </div>
 </div>

and so on.

2

Answers


  1. The easiest solution: Split the array into three (one for Item, Qty, and Price), and use a foreach with a named index variable. This works as long as the numerical index is consistent across all three nested arrays:

    <?php
    
    $itemArray = $array['item'];
    $qtyArray = $array['qty'];
    $priceArray = $array['price'];
    
    foreach($itemArray as $key => $item) {
        var_dump($item, $qtyArray[$key], $priceArray[$key]); // Note that $item === $itemArray[$key]
    }
    
    Login or Signup to reply.
  2. I know this isn’t an answer per se but I don’t know how in a comment to write the layout of the array.
    And so, all that to say that the organization of your data in arrays is very strange.
    Can’t you have a array of this type instead…?
    It would be much more consistent and easier to navigate to view the layers.

    Array
    (
        [0] => Array
            (
                [item] => Masaladosa
                [qty] => 2
                [price] => 320
            )
    
        [1] => Array
            (
                [item] => Zingafry
                [qty] => 4
                [price] => 800
            )
    
    )
    

    // An example to browse this array (nammed list)
    foreach($list as $key => $val)
    {
        echo "<br />".$key." - ".$val["item"]." - ".$val["qty"]." - ".$val["price"];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search