Jump to content

[SOLVED] XML to Array I'm in over my head :/


AV1611

Recommended Posts

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

I found the answer to the problem, but I could use some explaination of the answer  :P

 

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?

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?

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++;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.