2

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

2
  • Why don't you want to foreach() the array? Commented Feb 7, 2013 at 3:20
  • Why have foreach() when you can do it in 1 line? Commented Feb 7, 2013 at 3:24

2 Answers 2

9
if(!array_filter($array)){
  // empty
}

(docs)

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

2 Comments

+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)
That would be array_walk_recursive() (3D+)
3

It is also stated here

Check whether an array is empty

use array_filter();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.