doufu3718 2017-02-25 06:58
浏览 67
已采纳

如何做到这一点循环通过所有的孩子和子孩子,直到没有更多的孩子?

I want to loop thru all childs and sub childs till end of time. it starts from a HTML STRING, Some elements contains up to 7 or 8 layers of childs. How to do this the smart way?

<code>
include_once('simple_html_dom.php');
$style_array = array();
foreach(str_get_html($str)->find('*') as $element) {
$PARENT_NODE = new stdClass();
$PARENT_NODE->tag = $element->tag;
$PARENT_NODE->style = $element->style;
$PARENT_NODE->src = $element->src;
$PARENT_NODE->href = $element->href;
$PARENT_NODE->innertext = array();
if($element->hasChildNodes()) {
foreach(str_get_html($element->innertext)->find('*') as $element2) {
$CHILD_NODE_1 = new stdClass();
$CHILD_NODE_1->tag = $element2->tag;
$CHILD_NODE_1->style = $element2->style;
$CHILD_NODE_1->src = $element2->src;
$CHILD_NODE_1->href = $element2->href;
$CHILD_NODE_1->innertext = array();
if($element2->hasChildNodes()) {
foreach(str_get_html($element->innertext)->find('*') as $element3) {
$CHILD_NODE_2 = new stdClass();
$CHILD_NODE_2->tag = $element3->tag;
$CHILD_NODE_2->style = $element3->style;
$CHILD_NODE_2->src = $element3->src;
$CHILD_NODE_2->href = $element3->href;
$CHILD_NODE_2->innertext = $element3->innertext;
array_push($CHILD_NODE_1->innertext, $CHILD_NODE_2);
}
}else{
$CHILD_NODE_1->innertext = $element2->innertext;
}
array_push($PARENT_NODE->innertext, $CHILD_NODE_1);
}
}else{
$PARENT_NODE->innertext = $element->innertext;
}
array_push($style_array,array($PARENT_NODE));
};
echo var_export($style_array, true);  
</code>
  • 写回答

2条回答 默认 最新

  • dongqia0240 2017-02-25 08:25
    关注

    This is the best I can help you with, showing how to recurse over simplexml to build a standard array/object structure.

    $html = simplexml_load_string( file_get_contents('https://en.wikipedia.org/wiki/HTML5') );
    
    $output = process_html( $html );
    
    print_r($output);
    
    function process_html( SimpleXMLElement $nodes )
    {
      $array = array();
      /* @var $node SimpleXMLElement */
      foreach( $nodes as $node )
      {
        $object = new stdClass();
        $object->tag = $node->getName();
        $object->text = trim( (string) $node );
        if( $node->attributes() )
        {
          $object->attributes = new stdClass();
          foreach( $node->attributes() as $attrKey => $attr )
          {
            $object->attributes->{$attrKey} = (string) $attr;
          }
        }
        if( count( $node->children() ) )
        {
          // Here is the recursion
          $object->children = process_html( $node->children() );
        }
        $array[] = $object;
      }
      return $array;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题