ItSolutionStuff.com

PHP Check If Array Is Multidimensional or Not

By Hardik Savani • May 14, 2024
PHP

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

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...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Angular How to Remove Element from Array?

Read Now →

How to Remove Multiple Keys from PHP Array?

Read Now →

JQuery Check If Value Exists or Not in Array Example

Read Now →

JQuery - How to Push Specific Key and Value in Array?

Read Now →

How to Remove Specific Element by Value from Array in PHP?

Read Now →

How to Remove undefined Value from Array in JQuery?

Read Now →

How to Get Maximum Key Value of Array in PHP?

Read Now →

How to Remove Empty Values from Array in PHP?

Read Now →

How to Add Prefix in Each Key of PHP Array?

Read Now →

How to Get Minimum Key Value of Array in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →