I'm having a problem parsing the data being returned by my WCF web service.
The web service is passing back an array of strings, this data is put into a StdClass object, the problem i'm encountering is that the data changes depending on whether there is 1 or more objects.
Having never dealt with stdclass objects I'm not really sure what to do.
The following is the code i'm currently using, $containers is the return value from the web service call.
<ul>
<?php var_dump($containers)?>
<?php foreach($containers as $item):?>
<li>
<?php
echo $item->string;
?>
</li>
<?php endforeach;?>
</ul>
If there is just 1 value being returned then the following code works fine and displays the returned container name. If there is more than 1 value being returned $item->string becomes Array. is there anyway to determine what values stdclass contains?
var_dump with just 1 container
object(stdClass)[13]
public 'GetContainersResult' =>
object(stdClass)[14]
public 'string' => string 'container1' (length=10)
var_dump with more tha开发者_StackOverflow中文版n 1 container
object(stdClass)[13]
public 'GetContainersResult' =>
object(stdClass)[14]
public 'string' =>
array
0 => string 'container1' (length=10)
1 => string 'container2' (length=10)
Thanks in advance,
Matt
you can use is_array($item->string)
to check if you have an array and then process it appropriately. Based on your code, I think something like this might work for you.
<?php var_dump($containers)?>
<ul>
<?php foreach($containers as $item):
if(is_array($item->string)):
foreach($item->string as $subitem):
<li class="subitem"><?php echo $subitem; ?></li>
<?php
endforeach;
else: ?>
<li><?php echo $item->string; ?></li>
<?php
endif;
endforeach;
?>
</ul>
精彩评论