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

后端 未结 4 1099
忘了有多久
忘了有多久 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'])) ...
    

提交回复
热议问题