Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将第一个子元素解析为数组的简单html dom_Php_Arrays_Foreach_Simple Html Dom - Fatal编程技术网

Php 将第一个子元素解析为数组的简单html dom

Php 将第一个子元素解析为数组的简单html dom,php,arrays,foreach,simple-html-dom,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> <

考虑如下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>
相反,这是我得到的 因此,我的问题是:
如何获得所需的数组?

您的思路是正确的,唯一需要考虑的是将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);

您想要的是
$article->find
,而不是
$html->find
。明白为什么了吗?我不想成为批评家,但这是用foreach接近我需要的数值$1的糟糕方法,这是用于数组的。您可以只为($i=1;$i find('div.allItems>*'))执行
$i++{/…
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 )
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
        )

)