Given an array (single-dimensional or multi-dimensional) and the task is to check the given array is multi-dimensional or not. There are few methods to check an array is multi-dimensional or not. The function count() and count_recursive() will give wrong result if the array containing a sub-array which is empty, and the other one is using the rsort() function. This function sorts all the sub-arrays towards the beginning of the parent array and re-indexes the array. This ensures that if there are one or more sub-arrays inside the parent array, the first element of the parent array (at index 0) will always be an array. Checking for the element at index 0, we can tell whether the array is multidimensional or not.
rsort( $array )
Parameters: The rsort() function accepts one parameter.
- $array: This is the object you want to pass to the function.
Example 1: PHP program to check an array is multidimensional or not using rsort function.
Example 2: Another PHP program to check an array is multidimensional or not using count function.
Note: Try to avoid use count and count_recursive to check that the array is multi-dimension or not cause you may don’t know to weather the array containing a sub-array or not which is empty.