douqihua6212 2015-09-03 05:35
浏览 43
已采纳

PHP - 如何显示父数组的所有子项

Hi I am able to parse the array, however I am having trouble parse all sub item (children), it only shows first item of the children.

$xml_data = 
'<response>
<item>
  <name>Life Cover1</name>
  <feature>Indexation1</feature>
  <benefits>
    <bitem>foo1</bitem>
    <bitem>foo2</bitem>
  </benefits>
</item>
<item>
  <name>Life Cover2</name>
  <feature>Indexation2</feature>
  <benefits>
    <bitem>fd1</bitem>
    <bitem>foo3</bitem>
    <bitem>foo4</bitem>
  </benefits>
</item>
<item>
  <name>Life Cover3</name>
  <feature>Indexation3</feature>
  <benefits>
    <bitem>foo5</bitem>
    <bitem>foo6</bitem>
  </benefits>
</item>
</response>
';

//Display Data output format View
$xml = new SimpleXMLElement($xml_data);

$items = $xml->xpath('//item');
$itemNames = $xml->xpath('//name');
$features = $xml->xpath('//feature');
$benefits = $xml->xpath('//benefits');

$DataSize = sizeof($items);

echo '<table border="1" cellpadding="0" cellspacing="0"><tbody>';

foreach($items as $item){

  echo '<tr>';
  echo '<td>' . $item->name . '</td>';
  echo '<td>' . $item->feature . '</td>';
  echo '<td>' . $item->benefits->children() . '</td>';
  echo '</tr>';
}
echo '</tbody></table>';

The $item->benefits->children(); only show first bitem, how can display all bitem in this kind of parse output?

Thanks

  • 写回答

1条回答 默认 最新

  • dongqiang8058 2015-09-03 05:45
    关注

    You can try this :

    $xml_data = 
    '<response>
    <item>
      <name>Life Cover1</name>
      <feature>Indexation1</feature>
      <benefits>
        <bitem>foo1</bitem>
        <bitem>foo2</bitem>
      </benefits>
    </item>
    <item>
      <name>Life Cover2</name>
      <feature>Indexation2</feature>
      <benefits>
        <bitem>fd1</bitem>
        <bitem>foo3</bitem>
        <bitem>foo4</bitem>
      </benefits>
    </item>
    <item>
      <name>Life Cover3</name>
      <feature>Indexation3</feature>
      <benefits>
        <bitem>foo5</bitem>
        <bitem>foo6</bitem>
      </benefits>
    </item>
    </response>
    ';
    
    //Display Data output format View
    $xml = new SimpleXMLElement($xml_data);
    
    $items = $xml->xpath('//item');
    $itemNames = $xml->xpath('//name');
    $features = $xml->xpath('//feature');
    $benefits = $xml->xpath('//benefits');
    
    $DataSize = sizeof($items);
    
    echo '<table border="1" cellpadding="0" cellspacing="0"><tbody>';
    
    foreach($items as $item){
    
      echo '<tr>';
      echo '<td>' . $item->name . '</td>';
      echo '<td>' . $item->feature . '</td>';
      echo '<td>';
      $child_item = '';
      foreach($item->benefits->children() as $child)
      {
        $child_item .= $child.' ,';
      }
      echo rtrim($child_item,' ,');
      echo  '</td>';
      echo '</tr>';
    }
    echo '</tbody></table>';
    

    This will display all the child item with comma seperated string. Hope this will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突
  • ¥15 如何在线硕士了解,广告太多,希望有真实接触过的人回答下?(标签-学习|关键词-在线硕士)
  • ¥15 h5页面 使用u-upload 组件上传图片,如何解决,安卓系统不支持选择多张图片
  • ¥15 zabbix6.4与frp如何进行联动
  • ¥15 如何使用Echarts制作途中时间序列表
  • ¥15 图论相关的数学问题,共10个
  • ¥15 EtherCAT的问题,创建一个XML文件
  • ¥15 微信公众号羽毛球自动抢定场程序制作
  • ¥15 Fluent UDF 编写