tags:

views:

224

answers:

3

players will either be empty or a comma seperated list (or a single value). What is the easiest way to check if it's empty? I'm assuming I can do so as soon as I fetch the $gameresult array into $gamerow? In this case it would probably be more efficient to skip exploding the $playerlist if it's empty, but for the sake of argument, how would I check if an array is empty as well?

$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",",$gamerow['players']);

Thanks!

+1  A: 

count($gamerow['players']) will be 0.

Ignacio Vazquez-Abrams
A: 
empty($gamerow['players'])
Dan McGrath
+1  A: 

If you just need to check if there are ANY elements in the array

if(empty($playerlist))
{
     // list is empty.
}

If you need to clean out empty values before checking (generally done to prevent explodeing weird strings):

foreach($playerlist as $key => $value)
{
    if(empty($value))
    {
       unset($playerlist[$key];
    }
}
if(empty($playerlist))
{
   //empty array
}
Chacha102
Thanks a bunch!
aslum
Shouldn't you just use empty? count will take longer to perform for large arrays.
Dan McGrath
Done. I also changed it for the fact that you don't have to use isset and stuff.
Chacha102