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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵