PHP:Simple DOM Parser如何迭代这个html代码

问题描述:

<div id="score">
               <div class="name"><span style="width:68%">A</span></div>
                   <span class="roll">1</span>    

                <div class="name"><span style="width:60%">B</span></div>
                   <span class="roll">2</span>       

                 <div class="name"><span style="width:56%">C</span></div>
                   <span class="roll">3</span>          
 </div>

i want to iterate over each span of div.name but i am not getting to next span.roll tag I have used this code yet and also check the condition if A is available then 1 display and same as above name to check.

<?php
include("simple_html_dom.php");
$obj = new simple_html_dom();

 foreach ($obj->find('div[id=score]') as $factor)
    {
         $item = $factor->find('div[class=name] span')->plaintext;

          if(trim($item) == 'A')
          { 
             $a = $factor->find('span[class=roll]',0)->plaintext;
          }
          if(trim($item) == 'B')
          { 
            $b = $factor->find('span[class=roll]',1)->plaintext;
          }
          if(trim($item) == 'C')
          { 
             $c = $factor->find('span[class=roll]',2)->plaintext;
          }
           $final_array['overalldata'] = array
           (
            'a'=> $a,
            'b' => $b,
            'c' => $c,

            );
    }
   print_r($final_array);
   die;


?>

Any body having any idea please help to sort it out. Thanks

 &lt; div id =“score”&gt; 
&lt; div class =“name”&gt  ;&lt; span style =“width:68%”&gt; A&lt; / span&gt;&lt; / div&gt; 
&lt; span class =“roll”&gt; 1&lt; / span&gt;  
 
&lt; div class =“name”&gt;&lt; span style =“width:60%”&gt; B&lt; / span&gt;&lt; / div&gt; 
&lt; span class =“roll”&gt; 2&lt;  ; /跨度&GT;  
 
&lt; div class =“name”&gt;&lt; span style =“width:56%”&gt; C&lt; / span&gt;&lt; / div&gt; 
&lt; span class =“roll”&gt; 3&lt;  ; /跨度&GT;  
&lt; / div&gt; 
  code>  pre> 
 
 

我想迭代 div.name strong>的每个范围,但我没有进入下一个 span.roll strong>标签我已经使用了这段代码,并检查条件是否有A然后1显示和上面相同的名称来检查。 p>

   &lt;?php 
include(“simple_html_dom.php”); 
 $ obj = new simple_html_dom(); 
 
 foreach($ obj-&gt; find('div [id = score]')as $ factor)  
 {
 $ item = $ factor-&gt; find('div [class = name] span') - &gt; plaintext; 
 
 if(trim($ item)=='A')
 {  
 $ a = $ factor-&gt; find('span [class = roll]',0) - &gt; plaintext; 
} 
 if(trim($ item)=='B')
 {\  n $ b = $ factor-&gt; find('span [class = roll]',1) - &gt; plaintext; 
} 
 if(trim($ item)=='C')
 {
  $ c = $ factor-&gt; find('span [class = roll]',2) - &gt; plaintext; 
} 
 $ final_array ['overalldata'] = array 
(
'a'=&gt;  ; $ a,\  n'b'=&gt;  $ b,
'c'=&gt;  $ c,
 
); 
} 
 print_r($ final_array); 
 die; 
 
 
?&gt; 
  code>  pre> 
 
 

任何有任何想法的机构请帮忙解决。 谢谢 p> div>

As an alternative, since you're targeting that ID, you don't need to have a foreach on the parent element, just get it outright.

Then you apply the foreach on its children, getting the names and rolls.

Here's the idea:

$final_array['overalldata'] = null; // initialize
$factor = $obj->find('div[id=score]', 0); // get the parent
foreach ($factor->find('div[class=name]') as $item) { // each item of the parent
    $name = $item->find('span', 0)->innertext; // get the name
    $roll = $item->next_sibling()->innertext; // get the next sibling which is the roll
    $final_array['overalldata'][$name] = $roll; // and assign it (push it inside the array)

}
print_r($final_array);

So basically, to get the name just target the child inside each div, and then, to target the roll (which is the sibling of each div), just use ->next_sibling() method.

Sidenote: If I were you, I'd stick to DOM. It's already built-in inside PHP, no need to include any third party libraries.