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

后端 未结 4 1011
忘了有多久
忘了有多久 2021-02-01 15:11

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:



        
相关标签:
4条回答
  • 2021-02-01 15:16

    If you want to check $arr['dim1Key']['dim2Key']['dim3Key'], to be safe you need to check if all arrays exist before dim3Key. Then you can use array_key_exists.

    So yes, there is a simpler way using one single if statement like the following:

    if (isset($arr['dim1Key']['dim2Key']) &&
        array_key_exists('dim3Key', $arr['dim1Key']['dim2Key'])) ...
    
    0 讨论(0)
  • 2021-02-01 15:19

    For a fast one liner you can use has method from this array library:

    Arr::has('dim1Key.dim2Key.dim3Key')
    

    Big benefit is that you can use dot notation to specify array keys which makes things simpler and more elegant.

    Also, this method will work as expected for null value because it internally uses array_key_exists.

    0 讨论(0)
  • 2021-02-01 15:27

    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.
    0 讨论(0)
  • 2021-02-01 15:36

    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']
    
    0 讨论(0)
提交回复
热议问题