r/PHPhelp • • 1h ago

How to check if a multidimensional array key exists?

$smart_data['temperature']['current']

How can I check if that 2D key exists before trying to access it?

I had a whole long explanation of why I want to verify first but reddit ate the post with reddit server errors, ugh.

1 Upvotes

7 comments sorted by

4

u/sltrsd 1h ago

Check it with isset() or if you need also include null values use array_key_exists()

1

u/l008com 58m ago

I don't think I'm going to "GET" null values from the source. I think it will either be set to an integer, or it won't exist at all. So I can turn it into a null value. I think thats how it works, I'll use isset() and see what I get. Unfortunately I can't really test it and if it's working, I'll never really know if its working, or if the underlying program just hasn't messed up again :) good times.

1

u/HolyGonzo 1h ago

Usually you can use isset() which will check to see it exists AND isn't null.

If you just want to see if the key exists at all, then use array_key_exists().

If you're accessing it and want to get a default value if it doesn't exist or is null, then you can do:

$current_temperature = $smart_data['temperature']['current'] ?? -123;

That sets $current_temperature from $smart_data if it exists there, otherwise defaults to -123.

1

u/l008com 59m ago

array_key_exists says it only works on 1D arrays though.

1

u/HolyGonzo 45m ago

A multidimensional array is just an array of arrays.

$smart_data = [
  'foo' => 'bar', 
  'temperature' => [
    'current' => 50,
    'previous' => 49
  ]
];

var_dump(array_key_exists('foo', $smart_data));

var_dump(array_key_exists('current', $smart_data['temperature']));

var_dump(array_key_exists('future', $smart_data['temperature']));

1

u/heshamh911 58m ago

array_key_exists takes one key at a time, so for a nested key you chain it, and you have to guard the parent level or PHP warns about the missing first key:

if (array_key_exists('current', $smart_data['temperature'] ?? [])) { // the key is there, and the value may be null }

isset() walks the chain by itself, so it does not warn when a level is missing:

if (isset($smart_data['temperature']['current'])) { // the key is there and the value is not null }

So the only real difference is null. isset() returns false when the value is null, array_key_exists() returns true when the key exists with a null value.

For the usual "read it or use a default" case, ?? does the same walk as isset and is what I would use:

$current = $smart_data['temperature']['current'] ?? null;

The catch is that ?? treats an existing null the same as a missing key. If "not measured yet" and "not configured" are different states for you, use the array_key_exists version with the ?? [] guard instead.

In Laravel, Arr::has($smart_data, 'temperature.current') does the same check with dot notation and is true for keys holding null, and data_get() reads with a default when a segment is missing.

1

u/Takeoded 34m ago

If you can treat it's null and it's undefined as the same, it's trivial (!If you are unsure, this is what you actually want):

if(isset($smart_data['temperature']['current'])){
    // it exists! and it's not null!
}

If you need to treat it's defined as null as it's defined too, it's more difficult (this is probably not what you want):

if(isset($smart_data['temperature']) && array_key_exists('current', $smart_data['temperature'])){
    // it exists! it might be null, but it exist.
}