PHP Check If Array Is Multidimensional or Not

In this tutorial, we will discuss check if the array is multidimensional or not in PHP. This post will give you a simple example of PHP to 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 the array is a multidimensional PHP. Here, Creating a basic example of check if the array is a multidimensional PHP example.

Here, I will give you a very simple example of how to check if array is multidimensional or not in PHP. sometimes we need to check given array is a single-dimensional or multidimensional in PHP, so basically we can write code on according to the type of that array.

In this exampl we will create isMultiArray() with array argument. we have to pass array as argument and fuinction will check if array if multidimensional using rsort(), isset() and is_array() php function.

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

Example:

<?php
  
    $mySingleArray = [1, 2, 3, 4, 5];
   
    $myMultiArray = [
        ["id"=>1, "name"=>"america"],
        ["id"=>2, "name"=>"india"],
        ["id"=>3, "name"=>"canada"],
    ];
   
    var_dump(isMultiArray($mySingleArray));
   
    var_dump(isMultiArray($myMultiArray));
  
  
    function isMultiArray($arr) { 
        rsort($arr); 
        return isset($arr[0]) && is_array($arr[0]); 
    } 
  
?> 
Output:
bool(false)

bool(true)

we hope it can help you…

0 0 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x