Search code examples
phparraysis-empty

Check if an array is empty (not counting for keys)


I can't find out how to check if an array is empty. I know empty() means 100% empty, including keys. But my array looks like this when there are (in this case) no products:

Array
(
    [0] => 
)

How can I check if an array is empty like that? Preferably only for this exact "array list" because on a page that does have products I also have [0] => as first value, which I filter out (but this is after I need to check for the empty array).

Edit:

if(empty(array_values($relatedcr))){
    echo 'empty';
}else{
    echo 'not empty';
}

Solution

  • get the value as array, then check it.

    empty(array_values($array));
    

    Here is a test code:

    <?php 
    $array=[1];
    unset($array[0]);
    var_dump($array);
    var_dump(empty($array));
    var_dump(['']);
    var_dump(empty(['']));
    

    output: demo here

    array(0) {
    }
    bool(true)
    array(1) {
      [0]=>
      string(0) ""
    }
    bool(false)