开发者

Retrieve n Records From XML FIle With PHP

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 key开发者_JS百科s 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?


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....
}


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;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜