How to check an array is multidimensional or not in PHP ?
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.
Syntax:
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.
<?php $myarray = array( // Default key for each will // start from 0 array("Geeks", "For", "Geeks"), array("Hello", "World") ); // Function to check array is // multi-dimensional or not function is_multi_array( $arr ) { rsort( $arr ); return isset( $arr[0] ) && is_array( $arr[0] ); } // Display result var_dump( is_multi_array( $myarray ) ); ?> |
bool(true)
Example 2: Another PHP program to check an array is multidimensional or not using count function.
<?php // Declare an array $geeks = array(1 => 'a', 2 => 'b', 3 => array("Learn", "Contribute", "Explore")); $gfg = array(1 => 'a', 2 => 'b'); // Function to check array is // multi-dimensional or not function is_multi($geeks) { $rv = array_filter($geeks, 'is_array'); if(count($rv)>0) return true; return false; } // Display result var_dump(is_multi($geeks)); var_dump(is_multi($gfg)); ?> |
bool(true) bool(false)
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.
Recommended Posts:
- How to search by key=>value in a multidimensional array in PHP ?
- PHP multidimensional array search by value
- Multidimensional array in JavaScript
- Convert multidimensional array to XML file in PHP
- How to merge the duplicate value in multidimensional array in PHP ?
- Sort a multidimensional array by date element in PHP
- Multidimensional arrays in PHP
- Perl | Multidimensional Arrays
- Multidimensional Arrays in Java
- Perl | Multidimensional Hashes
- How to check whether an array is subset of another array using JavaScript ?
- How to check whether an array is empty using PHP?
- How to check if a variable is an array in JavaScript?
- How to check object is an array in JavaScript?
- Check if an array is empty or not in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



