0

I have the following array structure in place that i have to keep as it's an existing website:

$label_options = array(
    "label_size" => 'A4',
    "label_position" => 'Top',
    "label_qty" => 50
);

$ink_options = array(
    "black" => 1,
    "colour" => 0
);

$item = array(
    "item_number" => 12546518,
    "item_type" => 'Canon',
    "item_label_options" => $label_options,   
    "item_ink_options" => $ink_options,
    "item_qty" => 2,      
    "item_price" => 13.99,
    "item_total" => 26.98,
    "item_delivery" => 2.49
);

if (isset($_SESSION['cart'])) {
    $_SESSION['cart'][$unique] = $item;
} else {
    $_SESSION['cart'] = array($unique => $item);
}

I now want to be able to output the values inside the arrays and the arrays inside the array. I can use the below:

foreach ($_SESSION['cart'] as $unique => $item) {
    print 'Item Number = '.$item['item_number'].'<br />';
    print 'Qty = '.$item['item_qty'].'<br />';
    print 'Price = '.$item['item_price'].'<br />';

    foreach ($item['item_label_options'] as $key => $option) {
       print $option['label_size'].'<br />';
       print $option['label_position'].'<br />';
       print $option['label_qty'].'<br />';
    }
}

which successfully outputs the first loop but not the second - just putting the first letter of each:

Item Number = 12546518
Qty = 1
Price = 7.99

A // should be A4
T // should be Top
5 // should be 50

Also is there a way i can loop and output all the values without having to actually write 'Item Number = ... 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?

0

3 Answers 3

2

In your inner foreach loop, the keys label_size, label_position and label_qty don't exist. When a key doesn't exist, I guess it's defaulting to 0 and as a result just gives you the first letter. This will produce what you want:

foreach ($_SESSION['cart'] as $unique => $item) {
    print 'Item Number = '.$item['item_number'].'<br />';
    print 'Qty = '.$item['item_qty'].'<br />';
    print 'Price = '.$item['item_price'].'<br />';
    foreach ($item['item_label_options'] as $key => $option) {
       print $option.'<br />';
    }
}
0

why not print just $key and $value, this should print what you want: https://www.php.net/manual/de/control-structures.foreach.php

0

You are incorrectly accessing the $label_options array within $_SESSION['cart']. Within...

foreach ($item['item_label_options'] as $key => $option) {
   print $option['label_size'].'<br />';
   print $option['label_position'].'<br />';
   print $option['label_qty'].'<br />';
}

...$option is the array value, and not an array itself. You can easily verify this by echoing $key and $option. When you try and print $option['label_qty'] PHP essentially treats the string like an array, giving you the first letter.

You can access the items by key using:

foreach ($_SESSION['cart'] as $unique => $item) {
    echo 'Item Number = '.$item['item_number'].'<br />';
    echo 'Qty = '.$item['item_qty'].'<br />';
    echo 'Price = '.$item['item_price'].'<br />';
    echo $item['item_label_options']['label_size'];
    echo $item['item_label_options']['label_position'];
    echo $item['item_label_options']['label_qty'];
}
2
  • Thanks for your answer Dave, is there any way i can loop through all the item_label_options as i won't always know how many and what each one is called? Commented Dec 5, 2013 at 15:55
  • @odd_duck My answer does what you need. You just needed echo the value of $option itself. Commented Dec 5, 2013 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.