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

后端 未结 4 1004
忘了有多久
忘了有多久 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: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.

提交回复
热议问题