note 85184 deleted from function.array-key-exists by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 20:42:51 +0000 |
| Subject: | note 85184 deleted from function.array-key-exists by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218762@lists.php.net to get a copy of this message | ||
Note Submitter: Karim Ratib
----
Here's a function to return a reference to the first array element that has a given key. The
code works for multidimensional arrays:
<?php
function &array_find_element_by_key($key, &$form) {
if (array_key_exists($key, $form)) {
$ret =& $form[$key];
return $ret;
}
foreach ($form as $k => $v) {
if (is_array($v)) {
$ret =& array_find_element_by_key($key, $form[$k]);
if ($ret) {
return $ret;
}
}
}
return FALSE;
}
?>