php - foreach stdClass object converted to string -


i working response api returns data in json in straight forward structure. pseudo structure best represented as:

services -> service -> options -> option -> suboptions -> option 

as decoded json, of these stdclass objects. have number of foreach statements iterating through various services , options, work without issue. however, when use foreach statement @ suboption level, object serialized string. information suboptions has structure this.

[suboptions] => stdclass object  (     [option] => stdclass object         (             [code] => some_code             [name] => name         )   ) 

when using foreach such (where $option option in services -> service -> options -> option):

foreach($option->suboptions->option $suboption) {   print_r($suboption); } 

it outputs

some_codesome name 

not expected

stdclass object   (      [code] => some_code      [name] => name   ) 

i not aware of reasons foreach in terms of depth or other conditions. i've tried can think of , searched through , can't find case of happening. if has ideas i'd love hear them! cheers.

apologies if has been answered elsewhere or missing obvious. if have, has escaped me far.

edit: asking indeed auspost pac api. changing on drc being removed ~2014.

i'm assuming response australia post postage calculation api. api makes horrible decision treat collection instances 1 item single value. example, service -> options -> option may array or may single option.

the easiest way i've found deal cast option array, eg

$options = $service->options->option; if (!is_array($options)) {     $options = array($options); }  // can loop on safely foreach ($options $option) { ... } 

you same thing sub-options. if you're interested, i've got entire library deals auspost api, using guzzle manage http side.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -