2010-03-23 70 views
0

我有100條記錄一個XML文件,但我想它限制它僅僅5個記錄限制XML閱讀到5個記錄

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

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

當我把上面的代碼,它顯示一個記錄5次。

感謝 讓

回答

1
$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 
} 
0

看來,foreach循環只運行一次,因爲只有一個entry,並且for循環打印5次。如果有多個,則此代碼將打印每個條目5次。如果$xml->entry是一個數組,你可以做這樣的:

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

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

檢查是否有在XML文件中不止一個<entry>標籤。