2

I am seeking your help! I want to show the names of Player 2 and Player 3 whose disconnect_time is empty (because they're still in-game) and NOT show Player 1 because he's no longer in-game.

How would I go on about doing that?

I know about pulling out one name through

$xml->players->player->name;

but I don't know how to do it with multiple players
- and with an if statement like disconnect_time empty == show

<server>
<players>
   <player>
      <name>Player 1</name>
      <connect_time>2014-03-13 09:27</connect_time>
      <disconnect_time>2014-03-13 09:36</disconnect_time>
   </player>
   <player>
      <name>Player 2</name>
      <connect_time>2014-03-13 11:37</connect_time>
      <disconnect_time/>
   </player>
   <player>
      <name>Player 3</name>
      <connect_time>2014-03-13 11:45</connect_time>
      <disconnect_time/>
   </player>
</players>
</server>

Thank you so much in advance for any help!

3 Answers3

0

use this code

$xml = '<server>
<players>
   <player>
       <name>Player 1</name>
      <connect_time>2014-03-13 09:27</connect_time>
      <disconnect_time>2014-03-13 09:36</disconnect_time>
   </player>
   <player>
      <name>Player 2</name>
      <connect_time>2014-03-13 11:37</connect_time>
      <disconnect_time/>
   </player>
      <player>
      <name>Player 3</name>
      <connect_time>2014-03-13 11:37</connect_time>
      <disconnect_time>2014-03-13 09:36</disconnect_time>
   </player>
   </players>
</server>';
$xml = simplexml_load_string($xml);


 $array = $xml->players->player;
 $newarray = array(); 
foreach($array as $k => $v){ 

     if($v->disconnect_time == ""){ 
        $newarray[] = (string)$v->name;  
     }

}

print_r($newarray); 

Output :

Array ( [0] => Player 2 ) 
Ananth
  • 1,520
  • 3
  • 15
  • 28
0

Alternatively if you're not using the simplexml parser and are using a DOMDocument object, you can use the following:

$dom = new DOMDocument;
$dom->loadXML($xml);
$players = $dom->getElementsByTagName('player');
foreach($players as $player):
if($player->getElementsByTagName('disconnect_time')->item(0)->textContent == ''):
        echo $player->getElementsByTagName('name')->item(0)->textContent .' is still connected!';
    endif;
endforeach;
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
0

Using simpleXml:

$xml = simplexml_load_string($x); // assume XML in $x

get the players' names with xpathinstead of a loop:

$names = $xml->xpath("/server/players/player[disconnect_time = '']/name");

The expression is selecting the <name>-node of all <player>-nodes that have an empty <disconnect_time>-node. It's like SQL for XML. The results get stored as simpleXml-Elements in the array $names.

Now the output:

echo count($names) . " players are in the game: " . PHP_EOL;
foreach ($names as $name) echo $name . PHP_EOL;

see it working: https://eval.in/119697

michi
  • 6,565
  • 4
  • 33
  • 56
  • Works like a charm! Thank you! I really wish I could tag multiple answers as green ones. They're all great! What is PHP_EOL for? If you come back here, anyway :) – user3397543 Mar 13 '14 at 19:24
  • @user3397543: glad I could help. For `PHP_EOL`, see http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol – michi Mar 13 '14 at 20:21