Check If Array Is Multidimensional or Not PHP

    By: Thad Mertz
    2 years ago

    In this guide, we will find out how to check if array is multidimensional or not in php. In post we will find solution using simple example of php determine if array is multidimensional. We analyse example of php check array is multidimensional. i would like to share with you how to check if array is multidimensional php. Here let's Create a basic example of check if array is multidimensional php example.

    To check if array is multidimensional or not in php. sometime we need to check given array is a singledimensional or multidimensional in php, so basically we can write code on according to type of that array.


    So, here bellow simple example of checking array is multidimensional or not in php, let's see:


    <?php
    
      
    
        $singleArray = [1, 2, 3, 4, 5];
    
       
    
        $multiArray = [
    
            ["id"=>1, "name"=>"Rock"],
    
            ["id"=>2, "name"=>"John Cena"],
    
            ["id"=>3, "name"=>"Kane"],
    
        ];
    
       
    
        var_dump(isMultiArray($singleArray));     // Returns false;
    
       
    
        var_dump(isMultiArray($multiArray));      // returns true
    
      
    
      
    
        function isMultiArray($array) { 
    
            rsort($array); 
    
            return isset($array[0]) && is_array($array[0]); 
    
        } 
    
    ?> 
    

    In function isMultiArray we are simply checking if has first value of array as array, So if $array[0] is there and $array[0] is array. We have multi dimensional array.