开发者

Limiting XML read to 5 records

开发者 https://www.devze.com 2022-12-24 08:10 出处:网络
I have a xml file with 100 records, but I want it to limit it to just 5 records for ($i=0;$i<=5;$i++) {

I have a xml file with 100 records, but I want it to limit it to just 5 records

for ($i=0;$i<=5;$i++) {
   foreach($xml->entry as $result){ 

            if ($result->updated == $result->published) {
                    }
    }
}

When I put in the above code, it display one record 5 times.

Tha开发者_运维知识库nks Jean


$count = 0;
foreach($xml->entry as $result)
{
   if ($result->updated == $result->published) {
   }

   $count++;    
   if ($count++ == 5) break;
   // if ($count++ == 5) break; think this might work aswell
}


It seems that the foreach loop runs only once, because there is only one entry, and the for loop prints it 5 times. If there were more than one, this code would print each entry 5 times. If $xml->entry is an array, you can do it like this:

for($i = 0; $i < 5; $i++) {
    $result = $xml->entry[$i];

    if($result->updated == $result->published) {
    }
}

Check if there are more than one <entry> tags in your XML file.

0

精彩评论

暂无评论...
验证码 换一张
取 消