0
我正在使用XMLReader解析一個非常大的XML文件。緩衝或移動PHP數據以顯示在網頁的不同部分
$reader = new XMLReader;
$reader->open('products.xml');
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
while ($reader->read() && $reader->name !== 'product') {
continue;
}
while循環用於顯示基於用戶輸入的相關數據以及使用XML值構建數組。
while ($reader->name === 'product') {
$node = $dom->importNode($reader->expand(), TRUE);
if ($xpath->evaluate('number(price)', $node) > $price_submitted) {
$category = $xpath->evaluate('string(@category)', $node);
$name = $xpath->evaluate('string(name)', $node);
$price = $xpath->evaluate('number(price)', $node);
// Relevant search results displayed here:
echo "Category: " . $category . ". ";
echo "Name: " . $name . ". ";
echo "Price: " . $price . ". ";
$nameArray[] = $name;
}
$reader->next('product');
}
下面的foreach循環使用上面構建的while循環中的數組。我需要這些值(顯示在下面的foreach循環中)在while循環顯示相關搜索結果之前顯示。使用XMLReader,每次解析XML時只能執行一個while循環,而且由於內存和開銷問題,我不想再解析它兩次。做這個的最好方式是什麼?
foreach ($nameArray as $names) {
echo $names . " ";
}
謝謝!每頁只有10到20個搜索結果(包含類別,名稱和更多變量以及縮略圖),因此內存可能不會成爲問題。我一直在玩緩衝(IE ob_start(); ob_end_flush();),並一直無法讓它工作。你會考慮緩衝一個選項嗎? –
緩衝也會起作用,但緩衝需要更多的內存(所以我沒有建議它)。我會做一點不同的:1)。 'ob_start();'2)。 '$ contents = ob_get_flush();'3)。 'foreach(){...}'4)'echo $ contents;' – JimiDini