2017-02-25 84 views
0

我想循環所有的孩子和子孩子直到結束時間。它從一個HTML STRING開始,一些元素包含多達7或8層孩子。 如何做到這一點聰明的方式?如何做到這一點循環所有的孩子和子孩子,直到沒有更多的孩子?

<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> 
+0

你可以考慮使用[RecursiveIterator(http://docs.php.net/manual/da/class.recursiveiterator.php),或創建遞歸函數並在內部循環上引用它自己返回它所在的數據。 – Scuzzy

+0

是的,這是我想要做的,但我不知道如何開始這個,你認爲你可以幫忙嗎? –

+0

我想了解一下您的工作數據類型以及結果可能會有何幫助。有一個很好的機會有更好的方法來解決你所看到的任何問題。 – Scuzzy

回答

0

這是我可以幫助你的最好的,展示如何遞歸Simplexml以構建標準數組/對象結構。

$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

解決它謝謝Scuzzy

$html = str_get_html($str); 
$output = process_html($html); 
echo '<pre>' . var_export($output, true) . '</pre>'; 
function process_html($nodes) 
{ 
    $array = array(); 
    foreach(str_get_html($nodes)->find('*') as $node) { 
    $object = new stdClass(); 
    $object->tag = $node->tag; 
    $object->href = $node->href; 
    $object->src = $node->src; 
    $object->style = $node->style; 
    $object->innertext = $node->innertext; 
    if($node->hasChildNodes()) 
    { 
     $object->innertext = process_html($node->innertext); 
    } 
    $array[] = $object; 
    } 
    return $array; 
}