
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to check for multidimensional nature of an array in PHP
The ‘rsort’ function can be used to check if an array is multidimensional or not. It takes one parameter, i.e the array that needs to be checked and returns yes or no depending on the nature of the array.
Example
<?php $my_array = array( array("This", "is", "a", "sample"), array("Hi", "there") ); function multi_dim( $my_arr ) { rsort( $my_arr ); return isset( $my_arr[0] ) && is_array( $my_arr[0] ); } echo "Is the array multi-dimensional? "; var_dump( multi_dim( $my_array ) ); ?>
Output
Is the array multi-dimensional? bool(true)
An array is defined that contains string elements. A function named ‘multi_dim’ is defined that sorts the elements of the array using ‘rsort’. The ‘isset’ function is then used to perform ‘AND’ operation on the elements of the array. This would help understand if the array has a single dimension or is multi-dimensional.
- Related Articles
- PHP Multidimensional Array.
- How to convert Multidimensional PHP array to JavaScript array?
- Sort php multidimensional array by sub-value in PHP
- Sort multidimensional array by multiple keys in PHP
- How to check whether an array is empty using PHP?
- How do I sort a multidimensional array by one of the fields of the inner array in PHP?
- Multidimensional arrays in PHP
- How to print dimensions of multidimensional array in C++
- How can I most efficiently check for the existence of a single value in an array of thousands of values in PHP?
- How to re-index an array in PHP?
- Check for duplicates in an array in MongoDB?
- Multidimensional array in Java
- How to convert an array to SimpleXML in PHP?
- How To Access Different Rows Of A Multidimensional Numpy Array?
- How to do multidimensional array intersection using JavaScript?

Advertisements