note 126433 deleted from function.array-key-exists by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 20:41:32 +0000 |
| Subject: | note 126433 deleted from function.array-key-exists by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218754@lists.php.net to get a copy of this message | ||
Note Submitter: johannes dot kingma at gmail dot com
----
I often need a value from an array and if missing, some default. So this became a staple in every
utils.php file I include in my projects:
<?php
/**
* get array value if key exist otherwise default
* @param string $key needle
* @param mixed[] $array haystack
* @param mixed $default value if needle not found
*/
function array_value($key, $array, $default = '')
{
if (array_key_exists($key, $array))
return $array[$key];
return $default;
}
?>
Works great for $_POST and $_GET or for some $option array that I send to function of class
constructor.