3

I am in a very specific situation where I have an indexed array $name_array, which have 0 index but don't have values, as follow;

Array
(
    [0] => 
)

I want to consider this array as empty, I've checked it in isset($name_array) and !empty($name_array) but this array is passing both condition. I don't know What condition should I apply to detect if this array has these kind of values then it is empty?

for eg;

if(/*some condition*/($name_array)) 
{
    echo 'I am not empty';
}
else
{
    echo 'I am empty';
}
5
  • 7
    Possible duplicate of How to check if an array contains empty elements? Commented Feb 26, 2016 at 9:41
  • @Apb No, its not duplicate of above question, Both questions are completely different. Commented Feb 26, 2016 at 9:43
  • There are many links related to your question on SO. Google it. Commented Feb 26, 2016 at 9:47
  • stackoverflow.com/questions/5040811/… Commented Feb 26, 2016 at 9:49
  • 1
    $name_array = array_filter($name_array); Commented Feb 26, 2016 at 9:50

5 Answers 5

11

You can use array_filter:

if (!array_filter($name_array)) {
    // empty or has "empty" values
}

By default array_filter filters out any "empty" values like empty string, null or false.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I changed It as if (array_filter($name_array)) { echo 'I am not empty'; } else { echo 'I am empty'; } working as expected.
2

!empty is enough to check your first index of array is valid or not:-

if(!empty($array[0])){
 // $array[0] has value
}

In case of Multidimensional array where you don't know first index key,

if(!empty($array[key($array)])){
  echo $array[key($array)];
}

empty - Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:

"" (an empty string)

0 (0 as an integer)

0.0 (0 as a float)

"0" (0 as a string)

NULL

FALSE

array() (an empty array)

$var; (a variable declared, but without a value)

2 Comments

@devpro: In question, OP has defined index [0] of array. So I have suggested this way :)
yes, thats way i said, solution is fine... not ideal :)
0

There is no built-in function to check that
you can create your own instead :) here an example

function is_array_empty($name_array) {
    foreach($name_array as $elm) {
        if(!empty($elm)) return FALSE;
    }
    return TRUE;
}

Comments

0

Are you only trying to check if the first element is empty? if so:

if ( ! current($name_array)) {
    // first element in $name_array is niether false, null, 0 or an empty string
}

if you need to check a more specific case, just change the exclamation mark into whatever it is you need checked.

Comments

0
function is_array_empty($name_array) {
    $arrSize=sizeof($name_array);
    $emptElements=0;
    foreach($name_array as $elm) {
        if(empty($elm)){
           $emptElements++;
        }
    }
    if($emptElements==$arrSize){
        return TRUE;
    }else{
        return FALSE;
    }
}

1 Comment

too long... check @ksimka's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.