AV1611 Posted February 3, 2007 Share Posted February 3, 2007 ok, I've been trying to parse some xml. I have got the code this far foreach ($xmlobject->PLAYERLIST->PLAYER AS $playerid => $player) { echo "<pre>"; print_r($player); echo "</pre>"; now, I need to take the output of this statement's output and put it into a simple array. Here is the PRINT_R() of the above code: SimpleXMLElement Object ( [PLAYERID] => 11557 [PLAYERNAME] => {4F}BALDEAGLE [PLAYERSTATUS] => 1 [PLAYERSTATSURL] => http://aaotracker.com/usertracker.php?userid=11557 [PLAYERHONOR] => 100 [PLAYERTIME] => 11705846 [PLAYERSCORE] => 2186106 [PLAYERGOALSCORE] => 1400914 [PLAYERLEADERSCORE] => 468010 [PLAYERKILLS] => 50444 [PLAYERDEATHS] => 37037 ) How do I place the above into an array so I can call out elements later ($player[PLAYERSTATUS] ,etc...) OR can you call out the elements form the xml object above? Link to comment https://forums.phpfreaks.com/topic/36925-solved-xml-to-array-im-in-over-my-head/ Share on other sites More sharing options...
pocobueno1388 Posted February 3, 2007 Share Posted February 3, 2007 I think you can start calling them out like so: $player[0] That will print out the PLAYERID I think... Link to comment https://forums.phpfreaks.com/topic/36925-solved-xml-to-array-im-in-over-my-head/#findComment-176162 Share on other sites More sharing options...
AV1611 Posted February 3, 2007 Author Share Posted February 3, 2007 I found the answer to the problem, but I could use some explaination of the answer here is what works $PLAYERNAME=$player->PLAYERNAME; $PLAYERSTATUS=$player->PLAYERSTATUS; $PLAYERSTATSURL=$player->PLAYERSTATSURL; $PLAYERHONOR=$player->PLAYERHONOR; What I don't understand I guess is this: isn't $player->PLAYERSTATUS the same as $player[PLAYERSTATUS] ??? I tried ECHO $player[PLAYERSTATUS] but it gave an error... Explain? Link to comment https://forums.phpfreaks.com/topic/36925-solved-xml-to-array-im-in-over-my-head/#findComment-176168 Share on other sites More sharing options...
AV1611 Posted February 3, 2007 Author Share Posted February 3, 2007 Let me tell you what I want to do with this script. I want to sort the original xml list by an element in the xml. Here is where I am so far: <?php $clanid="20747"; $p=0; $xmllink = "http://aaotracker.com/livefeed/xml_clanprofile.php?clanid=$clanid"; $xmlstring = file_get_contents($xmllink); if ($xmlstring) { $xmlobject = simplexml_load_string($xmlstring); echo "<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\" class=\"tborder\">\n"; echo " <tr><td align=\"left\" class=\"catbg\">Member</td><td align=\"left\" class=\"catbg\">Status</td></tr>\n"; foreach ($xmlobject->PLAYERLIST->PLAYER AS $playerid => $player) { echo "<pre>"; // print_r($player); $PLAYERNAME=$player->PLAYERNAME; $PLAYERSTATUS=$player->PLAYERSTATUS; $PLAYERSTATSURL=$player->PLAYERSTATSURL; $PLAYERHONOR=$player->PLAYERHONOR; $PLAYERSORTLIST[$p][]=$PLAYERNAME; $PLAYERSORTLIST[$p][]=$PLAYERSTATUS; $PLAYERSORTLIST[$p][]=$PLAYERSTATSURL; $PLAYERSORTLIST[$p][]=$PLAYERHONOR; $p++; }} ECHO "<pre>"; print_r($PLAYERSORTLIST); echo "</pre>"; ?> this gives me this: Array ( [0] => Array ( [0] => SimpleXMLElement Object ( [0] => {4F}BALDEAGLE ) [1] => SimpleXMLElement Object ( [0] => 1 ) [2] => SimpleXMLElement Object ( [0] => http://aaotracker.com/usertracker.php?userid=11557 ) [3] => SimpleXMLElement Object ( [0] => 100 ) ) [1] => Array ( [0] => SimpleXMLElement Object ( [0] => {4F}Merlin ) [1] => SimpleXMLElement Object ( [0] => 0 ) [2] => SimpleXMLElement Object ( [0] => http://aaotracker.com/usertracker.php?userid=13046 ) [3] => SimpleXMLElement Object ( [0] => 62 ) ) I want to sort the list by array[][3] Did I make this too hard? is there a better way? I think I have to go multi-dimensional, right? Link to comment https://forums.phpfreaks.com/topic/36925-solved-xml-to-array-im-in-over-my-head/#findComment-176175 Share on other sites More sharing options...
AV1611 Posted February 4, 2007 Author Share Posted February 4, 2007 BUMP Link to comment https://forums.phpfreaks.com/topic/36925-solved-xml-to-array-im-in-over-my-head/#findComment-176522 Share on other sites More sharing options...
AV1611 Posted February 4, 2007 Author Share Posted February 4, 2007 Well, I figured out how to do what I wanted... The array_multisort() function doesn't work very well on a multidimensional array, but it seems to work good on multiple single arrays, so I broke the array into individual arrays then sorted like this: <?php $clanid="20747"; $p=0; $xmllink = "http://aaotracker.com/livefeed/xml_clanprofile.php?clanid=$clanid"; $xmlstring = file_get_contents($xmllink); if ($xmlstring) { $xmlobject = simplexml_load_string($xmlstring); foreach ($xmlobject->PLAYERLIST->PLAYER AS $playerid => $player) { $PSLN[]=$player->PLAYERNAME; $PSLS[]=$player->PLAYERSTATUS; $PSLU[]=$player->PLAYERSTATSURL; $PSLH[]=$player->PLAYERHONOR; $p++; } } $i=0; array_multisort($PSLH, $PSLN, $PSLS, $PSLU); $PSLH=array_reverse($PSLH); $PSLN=array_reverse($PSLN); $PSLS=array_reverse($PSLS); $PSLU=array_reverse($PSLU); while ($i<=($p-1)){ echo $PSLH[$i]." ".$PSLN[$i]." ".$PSLS[$i]." ".$PSLU[$i]."<br/>"; $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/36925-solved-xml-to-array-im-in-over-my-head/#findComment-176896 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.