Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to check if array like this

array(3) { [0]=> array(0) { } [1]=> array(0) { } [2]=> array(0) { } }

is actually empty ? Because for me this is an empty array but for the empty() this is an array with 3 elements and for count this is a array with length 3 .. so is there a way to do it without foreaching the array ?

Thank you in advance

share|improve this question
Why don't you want to foreach() the array? – esqew Feb 7 at 3:20
Why have foreach() when you can do it in 1 line? – Bankin Feb 7 at 3:24
add comment (requires an account with 50 reputation)

2 Answers

up vote 9 down vote accepted
if(!array_filter($array)){
  // empty
}

(docs)

share|improve this answer
Thanks a lot, really helped me :) – Bankin Feb 7 at 3:24
1  
+1 Clever. Note it will only work for two-dimensional arrays (like OP). Three dimensional arrays (array of arrays of arrays) would look something like array_walk($array, 'array_filter') && !array_filter($array) (untested) – Umbrella Feb 7 at 3:49
That would be array_walk_recursive() (3D+) – onetrickpony Feb 7 at 4:13
add comment (requires an account with 50 reputation)

It is also stated here

PHP. Check if an array is empty

use array_filter();

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.