PHP check if array is multidimensional or not

In this article explained of php check if array is multidimensional or not. This post will give you easy example of php determine if array is multidimensional. We will look at example of php check array is multidimensional.

I would like to share with you how to check if array is multidimensional or not. Here, basic example of check if array is multidimensional php example.

Sometime we need to check given array is a singledimensional or multidimensional in php, so basically we can write code according to type of that array. In this example we will create isMultiArray() with array argument. We have pass array as argument and function will check if array if multidimensional using rsort(), isset() and is_array() php function.

Let’see below example of checking array is multidimensional or not in php.

PHP check if array is multidimensional or not example:
<?php

$myArray = [1, 2, 3, 4, 5, 6];

$multiArray [
    ["id" => 1, "Grade" => "A"],
    ["id" => 2, "Grade" => "B"],
    ["id" => 3, "Grade" => "C"];
];

    var_dump(isMultiArray($myArray));
    
    var_dump(isMultiArray($multiArray));
    
    
    function isMultiArray($arrValue) {
        rsort($arrValue);
        return isset($arrValue[0]) && is_array($arrValue[0]);
    }
    


?>
Output:
bool(false)
bool(true)

I hope this tutorial help you.