Johanson
Johanson

Reputation: 5

Reading XML file in php only gives 1 result

im not that good at php but got this far some some google and help but are stuck. i use this to load the xml url

$xmls=simplexml_load_file("xmlurl",'SimpleXMLElement', LIBXML_NOCDATA);

it then outputs this.

Array
(
[0] => SimpleXMLElement Object
    (
        [state] => public
        [achievements] => SimpleXMLElement Object
            (
                [achievement] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [closed] => 1
                                    )
                                [name] => test1
                                [unlockTimestamp] => 1399400601
                            )

                        [1] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [closed] => 1
                                    )
                                [name] => test2
                                [unlockTimestamp] => 1399442265
                             )

when i echo the code it shows. i also want to put the unlocktimestamp there.

<?php 
$i = 0;
foreach($xmls as $node) {
$aname = $xmls[$i]->achievements->achievement[$i]->name ;
$i++;
echo "<pre>";
    print_r($aname);
    echo "</pre>"; 
}
?>

it only shows 1

SimpleXMLElement Object
(
[0] => test1
)

it doesnt show the rest that is in the sameobject.

did i forget something?

Edit:

i should note it loads multiply xml's in an array they are all the same structure. so i used the count at $xmls

$aname = $xmls[$i]->achievements->achievement[31]->name ;

when i do this it also only show only 1 entry but it does show name31

Upvotes: 0

Views: 48

Answers (1)

Marc B
Marc B

Reputation: 360662

Your first line inside the loop is incorrect. It should be:

foreach($xmls as $node) {
   foreach($node->achievements as $achievement) {
      echo '<pre>', $achievement->name, '</pre>';
   }
}

Right now you're trying to do

$top[0]->achievements[0]->name
$top[1]->achievements[1]->name
$top[2]->achievements[2]->name
$top[3]->achievements[3]->name
etc..

using the same variable for both array keys, regardless of how many (or few) achievements are actually in any node.

Upvotes: 1

Related Questions