PHP Tip: Check If Array Is Multidimensional

April 2, 2014
Edited: August 10, 2017

Here’s a handy little snippet that you can use to determine if the array you are about to work with is multidimensional or not.

if (count($array) == count($array, COUNT_RECURSIVE)) {
    /* not multidimentional */
} else {
    /* multidimensional */
}

By default, count() just counts the elements in the first level of the array. When you specify COUNT_RECURSIVE option, it loops through counting everything. If both match, it isn’t multidimensional.

Comments are closed