0
我有這樣的XML響應,我會做一個獲得18項第一個成果,一個SimpleXMLElement對象
foreach($response->entry->item as $data)
但$數據包含多個陣列(20)比我更需要(18)。所以我試圖做array_slice,但你知道它不會工作。
我可以嘗試其他解決方案嗎?
我有這樣的XML響應,我會做一個獲得18項第一個成果,一個SimpleXMLElement對象
foreach($response->entry->item as $data)
但$數據包含多個陣列(20)比我更需要(18)。所以我試圖做array_slice,但你知道它不會工作。
我可以嘗試其他解決方案嗎?
您可以嘗試限制從您通過xpath從xml中選擇的數據的結果集。
$string = <<<XML
<?xml version='1.0'?>
<document>
<item id="1" />
<item id="2" />
<item id="3" />
<item id="4" />
<item id="5" />
<item id="6" />
</document>
XML;
$xml = simplexml_load_string($string);
var_dump($xml->xpath("//item[@id>2 and @id<5]")) // Prints the two nodes matching the condition from the xpath
至於有人建議從註釋中,你也可以只是for
代替foreach
陣列上環和限制迭代次數。
我會嘗試一個爲@ k102建議,我認爲這是更好的方式。 TNX – Alex 2012-01-11 12:21:37
用'for'代替? – k102 2012-01-11 12:18:48
哈哈,很明顯...現在正在嘗試。非常感謝你。 – Alex 2012-01-11 12:19:49
你這樣做就像你會與任何其他陣列 – Gordon 2012-01-11 12:42:42