0

Hi i have an array coming from json data of placed order items in my api in this form. let's suppose this example :

[items] => Array
        (
            [0] => stdClass Object
                (
                 [item_id] => 392
                 [name] => ketchup 20g
                 [qty] => 2
                )

            [1] => stdClass Object
                (

                    [item_id] => 393
                    [name] => Powder 
                    [qty] => 1
                )

        )

-------------php code for dumping array data on screen -------

 $result=  json_decode($result);
 echo "<pre>";
 print_r($result);
 echo "</pre>";

........... Basically i wants to fetch all order items placed by customer in one order.

echo $result->items[0]->name;

this code is returning one item name at a time manually ,but let's suppose if one order has multiple items , how can i fetch it through loop? please guide

1

in my case due to array data are coming in multiple indexes so this worked for me: i hope it may help anybody

foreach ($result2 as  $value) {

      foreach($value as  $value2)
       {

        foreach($value2 as $value3)
           {
            foreach($value3->items as $value4)
               {
                    print_r($value4->name);
                    echo '<br>';
               }
           }
       }
}
0

This is a standard way to do it.

$jsondata = '';
$arr = (array)json_decode($jsondata, true);
 foreach ($arr as $k=>$v){
    echo $v; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.