array in a foreach using if statement?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Silentspy
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 5:53 pm

array in a foreach using if statement?

Post by Silentspy »

hi, im running a minecraft server and i got one mind boggling thing to ask regarding foreach, would really appreciate the help!

Code: Select all

function playeronlinelist($url)
{
if ($xml = simplexml_load_file($url)) {
$playerstats = objectsIntoArray($xml);
if( $playersonlinecount = count($playerstats["playersOnline"]) > 1 ) {
foreach ($playerstats["playerStats"] as $player) {
echo "<tr>";
echo "<td>";
echo $player["playerName"];
echo "</td>";
echo "<td>";
echo $player["blocksPlaced"];
echo "</td>";
echo "<td>";
echo $player["blocksDestroyed"];
echo "</td>";
echo "<td>";
echo $player["itemsDropped"];
echo "</td>";
echo "<td>";
echo $player["metersTraveled"];
echo "</td>";
echo "<td>";
echo $player["totalPlaytime"];
echo "</td>";
echo "</tr>";
}
} else {
echo "ingen online";
}
} else {
echo "klarte ikke å loade stats";
}
}
and the thing is, it will show everyone but the idea i had was to sort of filter out all the offline people using $player["isOnline"] in the foreach to filter out

anyone got a simple solution to this? ive tried searching around a ton and it seems like fair deal of mind stretching

i also tried this

Code: Select all

foreach ($playerstats["playerStats"] as $player) {
into

Code: Select all

foreach ($playerstats["playerStats"] as $player == $player["isOnline"] == "True") {
which ofcourse didnt work and with some thinking its quite wrong

here is a bit of the xml i turned into a array

Code: Select all

array(2) {
  ["playerStats"]=>
  array(56) {
    [0]=>
array(8) {
      ["blocksDestroyed"]=>
      string(2) "11"
      ["blocksPlaced"]=>
      string(3) "961"
      ["isOnline"]=>
      string(4) "true"
      ["itemsDropped"]=>
      string(2) "23"
      ["metersTraveled"]=>
      string(5) "22825"
      ["playerName"]=>
      string(9) "silentspy"
      ["playerSince"]=>
      string(29) "2010-11-19T23:58:58.406+01:00"
      ["totalPlaytime"]=>
      string(11) "14.95 hours"
    }
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: array in a foreach using if statement?

Post by Jonah Bron »

Just check it at the beginning of the loop. Use the continue command to skip if they're not online.

Code: Select all

foreach ($playerstats["playerStats"] as $player) {
    if ($player['isOnline'] != 'True') {
        continue;
    }
    echo "<tr>";
    echo "<td>";
    echo $player["playerName"];
    echo "</td>";
    echo "<td>";
    echo $player["blocksPlaced"];
    echo "</td>";
    echo "<td>";
    echo $player["blocksDestroyed"];
    echo "</td>";
    echo "<td>";
    echo $player["itemsDropped"];
    echo "</td>";
    echo "<td>";
    echo $player["metersTraveled"];
    echo "</td>";
    echo "<td>";
    echo $player["totalPlaytime"];
    echo "</td>";
    echo "</tr>";
}
Silentspy
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 5:53 pm

Re: array in a foreach using if statement?

Post by Silentspy »

thanks a lot! that worked perfectly! :D
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: array in a foreach using if statement?

Post by McInfo »

Be careful of this:

Code: Select all

if ($result = count($array) > 1)
Because assignment operators (=) have lower precedence than comparison operators (>), $result is the boolean (true or false) result of the comparison, not the return value of count().

If you want $result to be the return value of count(), use more parentheses.

Code: Select all

if (($result = count($array)) > 1)
Silentspy
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 5:53 pm

Re: array in a foreach using if statement?

Post by Silentspy »

thanks for the tip, i still got a lot to learn about php, but i feel its more a friendly language to get used to now !
Post Reply