Reputation: 33
PHP :
<?php
print_r($details);
?>
Hi friends i'm printing the value of $details in php code and the out put i got in browser is as follows.
Array (
[0] =>
Array ( [item_id] => 8 [row] => [merchant_id] => 1 [discount] => [currentController] => store [price] => 60|Large [qty] => 1 [notes] => [cooking_ref] => Cooking reference 1
[ingredients] => Array ( [0] => Ingredients 1 ) [require_addon_5] => 2
[sub_item] => Array (
[5] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
[6] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
[7] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
)
[addon_qty] => Array (
[5] => Array ( [0] => 1 [1] => 1 )
[6] => Array ( [0] => 1 [1] => 1 )
[7] => Array ( [0] => 1 [1] => 1 )
)
[require_addon_6] => 2
[require_addon_7] => 2
[two_flavors] =>
[non_taxable] => 2
[addon_ids] => Array ( [0] => 2 [1] => 3 [2] => 2 [3] => 3 [4] => 2 [5] => 3 )
)
)
Now I've to get the values of 'item_id' , 'cooking_ref' , sub_item and addon_qty as an individual array . Now I would like to know how can we access these values . I have tried something like
<?php
print_r($details);
echo "item_id : ".$details[0]['item_id']" <br />"
echo "price : ".$details[0]['price']" <br />"
echo "cooking_ref : ".$details[0]['cooking_ref']" <br />"
echo "cooking_ref : ".$details[0]['cooking_ref']" <br />"
echo "ingredients : "" <br />";
foreach($details['sub_item'] as $sub_item)
{
print_r($sub_item);
echo "<br /><br />";
$sub_item_array_val +=1;
}
?>
But didnt workedout for me could any one please suggest me how to acces these values from the above array , thanks in advance
Upvotes: 0
Views: 41
Reputation: 806
Here is one possible approach-
foreach($details as $detail)
{
echo "item_id: ".$detail['item_id']."<br/>";
echo "price: ".$detail['price']."<br/>";
echo "cooking_ref: ".$detail['cooking_ref']."<br/>";
foreach($detail[$sub_item] as $sub_item)
{
echo "ingredients: <br/>";
print_r($sub_item);
echo "<br/><br/>";
}
foreach($detail[$addon_qty] as $addon_qty)
{
echo "ingredients: <br/>";
print_r($addon_qty);
echo "<br/><br/>";
}
}
Upvotes: 0
Reputation: 1285
Foreach should be
foreach($details[0]['sub_item'] as $sub_item)
{
print_r($sub_item);
echo "<br /><br />";
}
Upvotes: 0
Reputation: 26288
Try this:
$details = // your array;
foreach($details as $detail)
{
echo $detail['item_id']; // will return 8
echo $detail['cooking_ref']; // will return Cooking reference 1
}
Upvotes: 0