0

I have an array called tagcat , like so

$tagcat = array();

....
while ( $stmt->fetch() ) {
    $tagcat[$tagid] = array('tagname'=>$tagname, 'taghref'=>$taghref);
}

Using print_r($tagcat) i get the following result set

   Array ( [] => Array ( [tagname] => [taghref] => ) )

Using var_dump($tagcat), i get

array(1) { [""]=> array(2) { ["tagname"]=> NULL ["taghref"]=> NULL } }

In php, i want to check if the array is empty. But when using the following conditions, it always finds something in the array, which is not true!

if ( isset($tagcat) ) {
    echo 'array is NOT empty';
} else {
    echo 'EMPTY!!!';
}

if ( !empty($tagcat) ) {
    echo 'array is NOT empty';
} else {
    echo 'EMPTY!!!';
}

How do i check if the array is empty?

Marco
  • 2,687
  • 7
  • 45
  • 61
  • 1
    What do you get if you do `print_r(tagcat);`. Also I'm assuming that you meant the `tagcat` to be a definition not a variable because you have no `$` in front of it. Is there more code to show? – Henders Jul 29 '16 at 11:36
  • 1
    @Henders fixed the typo + also edited the question to answer your question – Marco Jul 29 '16 at 11:38
  • 2
    Possible duplicate of [How to tell if a PHP array is empty?](http://stackoverflow.com/questions/2216052/how-to-tell-if-a-php-array-is-empty) – Abhishek Gurjar Jul 29 '16 at 11:39
  • Do you want to check if the array `$tagcat` is empty or if the array containing `tagname` and `taghref ` inside the array `$tagcat` is empty ? If you have nested arrays, be sure you're checking the correct array. – HiDeoo Jul 29 '16 at 11:41
  • @HiDeo i edited the question to answer your question – Marco Jul 29 '16 at 11:47
  • 1
    It's still unclear how exactly you define "empty". The array is clearly not empty according to PHP's definition of "empty". What's *your* definition exactly? – deceze Jul 29 '16 at 12:03

3 Answers3

3

Use array_filter

if(!array_filter($array)) {
  echo "Array is empty";
}

This was to check for the single array. For multi-dimensional array in your case. I think this should work:

$empty = 0;
foreach ($array as $val) {
  if(!array_filter($val)) {
    $empty = 1;
  }
}

if ($empty) {
  echo "Array is Empty";
}

If no callback is supplied, all entries of $array equal to FALSE will be removed.

With this it only returns the value which are not empty. See more in the docs example Example #2 array_filter() without callback

m13r
  • 2,458
  • 2
  • 29
  • 39
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
0

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

if (!empty($tagcat) ) { //its $tagcat, not tagcat
    echo 'array is NOT empty';
} else {
    echo 'EMPTY!!!';
}

Also, if you need to empty the values before checking

foreach ($tagcat as $cat => $value) {
    if (empty($value)) {
       unset($tagcat[$cat]);
    }
}
if (empty($tagcat)) {
   //empty array
}

Hope it helps

EDIT: I see that you have edited your $tagcat var. So, validate with vardump($tagcat) your result.

Marc Giroux
  • 186
  • 1
  • 7
  • 18
0
if (empty($array)) {
 // array is empty.
 }

if you want remove empty elements try this:

foreach ($array as $key => $value) {
  if (empty($value)) {
     unset($array[$key]);
  }
}
Khurian
  • 120
  • 1
  • 10