0

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

asked Dec 16, 2016 at 6:31
1
  • Try replacing $details['sub_item'] with $details[0]['sub_item'] Commented Dec 16, 2016 at 6:39

3 Answers 3

0

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/>";
 }
}
answered Dec 16, 2016 at 6:48
Sign up to request clarification or add additional context in comments.

Comments

0

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
}
answered Dec 16, 2016 at 6:40

Comments

0

Foreach should be

foreach($details[0]['sub_item'] as $sub_item)
 {
 print_r($sub_item);
 echo "<br /><br />";
 }
answered Dec 16, 2016 at 6:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.