-2

I've var dumped my array:

array(4) { [0]=> string(72) " 1 " [1]=> string(57) " 2 " [2]=> string(63) " › " [3]=> string(63) " » " } 

When I check:

empty($myArray); 

It always says true. Why is this?

3
  • 5
    Can you please show a bit more context? For instance, your debugging code. Commented Jun 30, 2014 at 9:54
  • then use count() to check the array is empty or not Commented Jun 30, 2014 at 9:54
  • Could you put exact code you use for creating array? Commented Jun 30, 2014 at 10:01

3 Answers 3

1

Try to use

count($myArray) == 0 

instead

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

Comments

-1
  • empty() function returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
  • The following things are considered to be empty, and return value would be true:
  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)

1 Comment

How does it help in this case? We all see array is not empty.
-1

Alternatively,you can check with count() function like this:-

if((count($myArray) < 1) || (count($myArray)) === 0){ echo 'Array is empty'; }else{ echo 'There is Data in Array'; } 

3 Comments

Not true. $myArray is variable, isn't it? Empty function works also for arrays: The following things are considered to be empty: array() (an empty array)
I didn't say you can't use it. but the empty function works some times delivers not the expected result. if used count(), with arrays that is more reliable. What i have wrote is the exact explanation on the php site. If you you are more knowledgeable in it. prove me wrong.
You can look for example at stackoverflow.com/questions/2216110/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.