PHP连接到WCF服务解析返回数据

PHP连接到WCF服务解析返回数据,php,stdclass,Php,Stdclass,解析WCF web服务返回的数据时遇到问题 web服务正在传回一个字符串数组,这些数据被放入一个StdClass对象中,我遇到的问题是,数据的变化取决于是否有一个或多个对象 由于从未处理过stdclass对象,我真的不知道该怎么办 下面是我当前使用的代码,$containers是web服务调用的返回值 <ul> <?php var_dump($containers)?> <?php foreach($containers as $item):?> <li

解析WCF web服务返回的数据时遇到问题

web服务正在传回一个字符串数组,这些数据被放入一个StdClass对象中,我遇到的问题是,数据的变化取决于是否有一个或多个对象

由于从未处理过stdclass对象,我真的不知道该怎么办

下面是我当前使用的代码,$containers是web服务调用的返回值

<ul>
<?php var_dump($containers)?>
<?php foreach($containers as $item):?>
<li>
<?php 
echo $item->string;
?>
</li>
<?php endforeach;?>
</ul>
带有多个容器的var_转储

object(stdClass)[13]
public 'GetContainersResult' => 
object(stdClass)[14]
public 'string' => string 'container1' (length=10)
object(stdClass)[13]
public 'GetContainersResult' => 
object(stdClass)[14]
public 'string' => 
array
0 => string 'container1' (length=10)
1 => string 'container2' (length=10)
提前感谢,

Matt

您可以使用is_array$item->string检查是否有数组,然后适当地处理它。根据你的代码,我认为这样的东西可能适合你

<?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>

感谢您的帮助,需要将foreach语句更改为“foreach$item->string as$subitem:”,并将li语句更改为乐于提供帮助。感谢您的反馈,我更新了代码以确保正确。