tags:

views:

58

answers:

2

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&lt;/picture&gt;
        <link>http://www.site.com&lt;/link&gt;

    </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?

+2  A: 

Can I use a for loop possibly? Or use a count variable somehow?

for($i = 0; $i < count($xml->info); $i++)
{
  // your code....
}

Update:

Use this if you want to limit to 6:

for($i = 0; $i <= 6; $i++)
{
  // your code....
}
Sarfraz
I need six, your code returns 300 keys.
rrrfusco
@rrrfusco: then add your own limit? (N instead of `Count(...`
Wrikken
@rrrfusco: See my updated answer please.
Sarfraz
I tried to put a for loop around the foreach loop, with no success. I'm having trouble with nested nodes.
rrrfusco
@rrrfusco: _replace_ the foreach loop, don't nest it.
Wrikken
A: 

Edit
I'm having difficulty placing a for loop along with the foreach for nested nodes.


Answer to Retrieve n Records

function getItems($file_id, $item_count=null)
{
    switch ($file_id)
    {
        case '2':
        $file = "http://xml_file.xml";

        if ($xml = simplexml_load_file($file)) 
        {
            $i=0;
            foreach ($xml->info as $info) 
            {
                if ($i < $item_count)
                {
                    $var[] = array(
                        "Name"  =>  (string)$info->name,
                        "Image" => (string)$info->picture); 
                }
                $i++;   
            }
            return $var;
        }
    }
}

Can anyone suggest how to get n random records from 300 records?


Answer to Random Records

function getItems($file_id, $item_count=null)
{
    switch ($file_id)
    {
        case '2':
        $file = "http://xml_file.xml";

        if ($file) 
        {
            $xml = simplexml_load_file($file);

            $k = array();
            for ($i=0; $i<$item_count; $i++)
            {
                $k[] = rand(1,300)
            }

            $i=0;
            foreach ($xml->info as $info) 
            {
                if ($i < $item_count)
                {
                    if (in_array($i, $k))
                    {
                        $var[] = array(
                            "Name"  =>  (string)$info->name,
                            "Image" => (string)$info->picture);
                    } 
                }
                $i++;   
            }
            return $var;
        }
    }
}
rrrfusco