Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to check whether an array is empty using PHP?

players will either be unfilled or a comma-isolated list (or a single value). What is the least demanding approach to check if it's empty? I'm expecting I can do as such when I bring the $gameresult array into $gamerow? For this situation it would most likely be more productive to skip exploding the $playerlist if it's blank, however for contention, how might I check if an array is empty too?
$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);
by

2 Answers

akshay1995
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
}
sandhya6gczb
Use this code to check whether an array is empty or not.

<?php
$playerList = array();
if (!$playerList) {
echo "No players";
} else {
echo "Explode stuff...";
}
// Output is: No players

Login / Signup to Answer the Question.