PHP - 如何显示soap响应子子列表

时间:2015-10-09 01:14:22

标签: php xml

我收到了以下xml格式的回复,如下所示: 如何在一行或容器中获取list-> item->值:

<list>
   <item>
    <Key>3</Key>
    <Value>3960</Value>
</item>
<item>
    <Key>5</Key>
    <Value>3967</Value>
</item>
<item>
    <Key>6</Key>
    <Value>3968</Value>
</item>
</list>

如何显示如下所示的值

<table>
<tr>
<td>3960, 3967, 3968</td>
<td>3963, 3961, 3960</td>
</tr>
</table>

并且此刻我尝试在foreach中使用children(),但它返回错误:在null上调用成员函数children(),下面是我的php代码

foreach($items as $item){

echo '<td>';
  $child_item = '';
  foreach($item->list->children()->children() as $child)
  {
    $child_item .= $child .' ,';
  }
  echo rtrim($child_item,' ,');
  echo  '</td>';

  }

感谢专家!

1 个答案:

答案 0 :(得分:0)

这是您需要实现的目标:

<?php
$xmlstr = <<<XML
<root>
    <list>
        <item>
            <Key>3</Key>
            <Value>3960</Value>
        </item>
        <item>
            <Key>5</Key>
            <Value>3967</Value>
        </item>
        <item>
            <Key>6</Key>
            <Value>3968</Value>
        </item>
    </list>
    <list>
        <item>
            <Key>3</Key>
            <Value>3963</Value>
        </item>
        <item>
            <Key>5</Key>
            <Value>3961</Value>
        </item>
        <item>
            <Key>6</Key>
            <Value>3960</Value>
        </item>
    </list>
</root>
XML;

$items = new SimpleXMLElement($xmlstr);

echo "<table>\r\n";
echo "<tr>\r\n";
foreach($items as $list){
    echo "<td>";
    $itemsArr = array();
    foreach($list as $item){
        $itemsArr[] = $item->Value[0];
    }
    echo implode(", ", $itemsArr);
    echo "</td>\r\n";
}
echo "</tr>\r\n";
echo "</table>";
?>