简单的html dom将第一个子解析成数组

时间:2016-03-29 05:06:07

标签: php arrays foreach simple-html-dom

考虑一下html代码如下

<html>
<div class="allItems">
   <div class="Item" id="12345">
      <div class="ItemName">Tom</div>
      <div class="ItemAge">34</div>
      <div class="ItemGender">male</div>
   </div>
   <div class="Item" id="17892">
      <div class="ItemName">Dick</div>
      <div class="ItemAge">23</div>
      <div class="ItemGender">male</div>
   </div>
   <div class="Item" id="98776">
      <div class="ItemName">Harry</div>
      <div class="ItemAge">65</div>
      <div class="ItemGender">male</div>
   </div>
</div>
</html>

我正在尝试解析这个html文档,如下所示:

<?php

include_once("/simple_html_dom.php");

<--a bunch of code that works until here -->

$html = str_get_html($str); //this pulls the html code fine

//this is where my array constructions does not work
foreach ($html->find('div.allItems > *') as $article) {
   $item['name'] = $html->find('div.ItemName', 0)->plaintext;
   $item['age'] = $html->find('div.ItemAge',0)->plaintext;
   $item['gender'] = $html->('div.ItemGender', 0)->plaintext;
   $articles[] = $item;
}

print_r($articles);

?>

我期望得到什么:

Array ( [0] => Array ( [name] => Tom [Age] => 34 [Gender] => male ) 
[1] => Array ( [name] => Dick [Age] => 23 [Gender] => male ) 
[2] => Array ( [name] => Harry [Age] => 65 [Gender] => male )

相反,这就是我得到的

Array ( [0] => Array ( [name] => Tom [Age] => 34 [Gender] => male ) 
[1] => Array ( [name] => Tom [Age] => 34 [Gender] => male ) 
[2] => Array ( [name] => Tom [Age] => 34 [Gender] => male )

我的问题因此:

如何获得所需的阵列?

1 个答案:

答案 0 :(得分:1)

你走在正确的轨道上,唯一的想法是你需要将div的索引设置为find函数的第二个参数。看看下面的解决方案:

include_once("simple_html_dom.php");
$str = '<html>
<div class="allItems">
   <div class="Item" id="12345">
      <div class="ItemName">Tom</div>
      <div class="ItemAge">34</div>
      <div class="ItemGender">male</div>
   </div>
   <div class="Item" id="17892">
      <div class="ItemName">Dick</div>
      <div class="ItemAge">23</div>
      <div class="ItemGender">male</div>
   </div>
   <div class="Item" id="98776">
      <div class="ItemName">Harry</div>
      <div class="ItemAge">65</div>
      <div class="ItemGender">male</div>
   </div>
</div>
</html>';

$html = str_get_html($str); //this pulls the html code fine

//this is where my array constructions does not work
$i = 0;
foreach ($html->find('div.allItems > *') as $article) {
    $item['name'] = $html->find('div.ItemName', $i)->plaintext;
    $item['age'] = $html->find('div.ItemAge',$i)->plaintext;
    $item['gender'] = $html->find('div.ItemGender', $i)->plaintext;
    $articles[] = $item;
    $i++;
}

print_r($articles);

<强>输出:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 34
            [gender] => male
        )

    [1] => Array
        (
            [name] => Dick
            [age] => 23
            [gender] => male
        )

    [2] => Array
        (
            [name] => Harry
            [age] => 65
            [gender] => male
        )

)