I'm trying to retrieve N number of items from an XML file using simpleXML and put the information into a 2 dimensional array like:
[0][name]
[0][image]
[1][name]
[1][image]
[2][name]
[2][image]
In this case N items will be 6.
I'd like to do this two ways,
1. Grab the first 0-6 keys and values
2. Or a random 6 from the xml file.
The xml document has 300 Records.
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>
This is what I have so far. Reading the xml produces a 2 dimensional array:
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;
}
}
}
Can I use a for loop possibly? Or use a count variable somehow?