- What is the most efficient way to check if an array is a flat array of primitive values or if it is a multidimensional array?
- Is there any way to do this without actually looping through an array and running
is_array()on each of its elements?
What is the most efficient way to check if an array is a flat
Share
The short answer is no you can’t do it without at least looping implicitly if the ‘second dimension’ could be anywhere. If it has to be in the first item, you’d just do
But, the most efficient general way I could find is to use a foreach loop on the array, shortcircuiting whenever a hit is found (at least the implicit loop is better than the straight for()):
Implicit looping, but we can’t shortcircuit as soon as a match is found…