note 128747 added to function.array-key-exists
| From: | Rumour at main dot php dot net | Date: | Tue, 18 Jul 2023 14:55:35 +0000 |
| Subject: | note 128747 added to function.array-key-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220707@lists.php.net to get a copy of this message | ||
In PHP7+ to find if a value is set in a multidimensional array with a fixed number of dimensions,
simply use the Null Coalescing Operator: ??
So for a three dimensional array where you are not sure about any of the keys actually existing
<?php
// instead of:
$exists = array_key_exists($key1, $arr) && array_key_exists($key2, $arr[$key1]) &&
array_key_exists($key3, $arr[$key1][$key2]) ;
// use:
$exists = array_key_exists($key3, $arr[$key1][$key2]??[]) ;
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.168 (proxied: 85.145.75.206)
----
Manual Page -- https://php.net/manual/en/function.array-key-exists.php
Edit -- https://main.php.net/note/edit/128747
Del: integrated -- https://main.php.net/note/delete/128747/integrated
Del: useless -- https://main.php.net/note/delete/128747/useless
Del: bad code -- https://main.php.net/note/delete/128747/bad+code
Del: spam -- https://main.php.net/note/delete/128747/spam
Del: non-english -- https://main.php.net/note/delete/128747/non-english
Del: in docs -- https://main.php.net/note/delete/128747/in+docs
Del: other reasons-- https://main.php.net/note/delete/128747
Reject -- https://main.php.net/note/reject/128747
Search -- https://main.php.net/manage/user-notes.php