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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序