Note that there are some explanatory texts on larger screens.

plurals
  1. POphp loop through array within array - output array value and key
    primarykey
    data
    text
    <p>I have the following array structure in place that i have to keep as it's an existing website:</p> <pre><code>$label_options = array( "label_size" =&gt; 'A4', "label_position" =&gt; 'Top', "label_qty" =&gt; 50 ); $ink_options = array( "black" =&gt; 1, "colour" =&gt; 0 ); $item = array( "item_number" =&gt; 12546518, "item_type" =&gt; 'Canon', "item_label_options" =&gt; $label_options, "item_ink_options" =&gt; $ink_options, "item_qty" =&gt; 2, "item_price" =&gt; 13.99, "item_total" =&gt; 26.98, "item_delivery" =&gt; 2.49 ); if (isset($_SESSION['cart'])) { $_SESSION['cart'][$unique] = $item; } else { $_SESSION['cart'] = array($unique =&gt; $item); } </code></pre> <p>I now want to be able to output the values inside the arrays and the arrays inside the array. I can use the below:</p> <pre><code>foreach ($_SESSION['cart'] as $unique =&gt; $item) { print 'Item Number = '.$item['item_number'].'&lt;br /&gt;'; print 'Qty = '.$item['item_qty'].'&lt;br /&gt;'; print 'Price = '.$item['item_price'].'&lt;br /&gt;'; foreach ($item['item_label_options'] as $key =&gt; $option) { print $option['label_size'].'&lt;br /&gt;'; print $option['label_position'].'&lt;br /&gt;'; print $option['label_qty'].'&lt;br /&gt;'; } } </code></pre> <p>which successfully outputs the first loop but not the second - just putting the first letter of each:</p> <pre><code>Item Number = 12546518 Qty = 1 Price = 7.99 A // should be A4 T // should be Top 5 // should be 50 </code></pre> <p>Also is there a way i can loop and output all the values without having to actually write <code>'Item Number = ...</code> it can just output each key with its value as i won't always know how many and what the names of each one are?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload