HDTuto.com

How to Check if Array is Multidimensional or Not in PHP?



This tutorial shows you php check array is multidimensional. In this article, we will implement a how to check if array is multidimensional php. if you have question about check if array is multidimensional php example then i will give simple example with solution. this example will help you check if array is multidimensional or not in php.

Here, i will give you very simple example of how 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.

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"=>"Hardik"],

["id"=>2, "name"=>"Paresh"],

["id"=>3, "name"=>"Naresh"],

];

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)

I hope it can help you...




May 13, 2020 | Category : PHP



Blog Search