Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: testing for existence of a cell in a multidimensional array

I have an array with numerous dimensions, and I want to test for the existence of a cell.

The below cascaded approach, will be for sure a safe way to do it:

if (array_key_exists($arr, 'dim1Key'))     if (array_key_exists($arr['dim1Key'], 'dim2Key'))       if (array_key_exists($arr['dim1Key']['dim2Key'], 'dim3Key'))         echo "cell exists";   

But is there a simpler way?

I'll go into more details about this:

  1. Can I perform this check in one single statement?
  2. Do I have to use array_key_exist or can I use something like isset? When do I use each and why?
like image 717
shealtiel Avatar asked Jan 02 '11 01:01

shealtiel


People also ask

How do you check if a value exists in an array in PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.

How do you check an array is multidimensional or not in PHP?

Checking for the element at index 0, we can tell whether the array is multidimensional or not. Parameters: The rsort() function accepts one parameter. $array: This is the object you want to pass to the function.

How do you calculate the total number of elements in a multidimensional array in PHP?

Parameters ¶ If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

How do you find the value of multidimensional array?

Size of multidimensional arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.


2 Answers

isset() is the cannonical method of testing, even for multidimensional arrays. Unless you need to know exactly which dimension is missing, then something like

isset($arr[1][2][3]) 

is perfectly acceptable, even if the [1] and [2] elements aren't there (3 can't exist unless 1 and 2 are there).

However, if you have

$arr['a'] = null; 

then

isset($arr['a']); // false array_key_exists('a', $arr); // true 

comment followup:

Maybe this analogy will help. Think of a PHP variable (an actual variable, an array element, etc...) as a cardboard box:

  • isset() looks inside the box and figures out if the box's contents can be typecast to something that's "not null". It doesn't care if the box exists or not - it only cares about the box's contents. If the box doesn't exist, then it obviously can't contain anything.
  • array_key_exists() checks if the box itself exists or not. The contents of the box are irrelevant, it's checking for traces of cardboard.
like image 121
Marc B Avatar answered Oct 05 '22 09:10

Marc B


I was having the same problem, except i needed it for some Drupal stuff. I also needed to check if objects contained items as well as arrays. Here's the code I made, its a recursive search that looks to see if objects contain the value as well as arrays. Thought someone might find it useful.

function recursiveIsset($variable, $checkArray, $i=0) {     $new_var = null;     if(is_array($variable) && array_key_exists($checkArray[$i], $variable))         $new_var = $variable[$checkArray[$i]];     else if(is_object($variable) && array_key_exists($checkArray[$i], $variable))         $new_var = $variable->$checkArray[$i];     if(!isset($new_var))         return false;      else if(count($checkArray) > $i + 1)         return recursiveIsset($new_var, $checkArray, $i+1);     else         return $new_var; } 

Use: For instance

recursiveIsset($variables, array('content', 'body', '#object', 'body', 'und')) 

In my case in drupal this ment for me that the following variable existed

$variables['content']['body']['#object']->body['und'] 

due note that just because '#object' is called object does not mean that it is. My recursive search also would return true if this location existed

$variables->content->body['#object']->body['und'] 
like image 42
user1327498 Avatar answered Oct 05 '22 07:10

user1327498