2012-01-11 39 views
0

我有这样的XML响应,我会做一个获得18项第一个成果,一个SimpleXMLElement对象

foreach($response->entry->item as $data) 

但$数据包含多个阵列(20)比我更需要(18)。所以我试图做array_slice,但你知道它不会工作。

我可以尝试其他解决方案吗?

+1

用'for'代替? – k102 2012-01-11 12:18:48

+0

哈哈,很明显...现在正在尝试。非常感谢你。 – Alex 2012-01-11 12:19:49

+0

你这样做就像你会与任何其他阵列 – Gordon 2012-01-11 12:42:42

回答

1

您可以尝试限制从您通过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阵列上环和限制迭代次数。

+0

我会尝试一个为@ k102建议,我认为这是更好的方式。 TNX – Alex 2012-01-11 12:21:37