我試圖檢索使用SimpleXML從XML文件中的項目N多,把信息變成一個2維數組,如:檢索N條記錄從XML文件PHP
[0][name]
[0][image]
[1][name]
[1][image]
[2][name]
[2][image]
在這種情況下N個項目將6
我願意做這兩種方式,
1.搶第0-6鍵和值
2.或者從XML文件的隨機6。
該xml文檔有300條記錄。
XML Example:
<xml version="1.0">
<info>
<no>1</no>
<name>Name</name>
<picture>http://www.site.com/file.jpg</picture>
<link>http://www.site.com</link>
</info>
</xml>
這是我到目前爲止。讀取xml會產生一個二維數組:
function getItems($file_id, $item_count=null)
{
switch ($file_id)
{
case '2':
$file = "http://xml_file.xml";
if ($xml = simplexml_load_file($file))
{
foreach ($xml->info as $info)
{
$var[] = array(
"Name" => (string)$info->name,
"Image" => (string)$info->picture);
}
return $var;
}
}
}
我可以使用for循環嗎?或者以某種方式使用計數變量?
可以請你發佈一些XML與該代碼? – Gordon 2010-08-02 13:48:29
第二代碼塊從頂部... – rrrfusco 2010-08-02 14:48:36