I'm getting tired of writing ternary expressions to sanitize the data, things like:
$x = isset($array['idx']) ? $array['idx'] : null; // and $x = !empty($array['idx']) ? $array['idx'] : null;
Is there a native way or ZF accessor/filter to get array element value for some given array without:
error_reporting
isset
/empty
check @
Something like:
$x = get_if_set($array['idx']); // or $x = Zend_XXX($array, 'idx')
Access Array Elements Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Undefined value primitive value is used when a variable has not been assigned a value. The standard clearly defines that you will receive undefined when accessing uninitialized variables, non-existing object properties, non-existing array elements, and alike.
Use the Array. includes() method to check if an array contains undefined values, e.g. arr. includes(undefined) . The includes method will return true if the array contains at least one undefined value and false otherwise.
To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.
PHP7 introduced the null coalesce operator ??
. Assuming you're lucky enough to be running it, you can just do
$x = $array['idx'] ?? null;
Things are much more easy - thanks to Andrea Faulds and Nikita Popov for - the Null Coalescing Operator ??
Docs, Migration, RFC:
$x = $array['idx'] ?? NULL;
or with a
$default
:$x = $array['idx'] ?? $default ?? NULL;
Like isset
or empty
it gives no warning and the expression falls through to the right (if unset). if set and not NULL the value is taken. This is also how $default
in the previous example works, always, even if undefined.
Thanks to Midori Kocak - is the Null Coalescing Assignment Operator ??=
Docs, RFC (which was one of the things I missed after ??
) allows to assign default values directly:
$array['idx'] ??= null; $x = $array['idx'];
I don't use it by far as often as ??
, but it is good to know about it, especially if while break up data-handling logic and want to have defaults early.
As far as you only need NULL as "default" value, you can make use of the error suppression operator:
$x = @$array['idx'];
Criticism: Using the error suppression operator has some downsides. First it uses the error suppression operator, so you can not easily recover issues if that part of the code has some. Additionally the standard error situation if undefined does pollute looking for screams. Your code is not as expressing itself as precise as it could be. Another potential issue is to make use of an invalid index value, e.g. injecting objects for indexes etc.. This would get unnoticed.
It will prevent warnings. However if you like to allow other default values as well, you can encapsulate the access to an array offset via the ArrayAccess
interface:
class PigArray implements ArrayAccess { private $array; private $default; public function __construct(array $array, $default = NULL) { $this->array = $array; $this->default = $default; } public function offsetExists($offset) { return isset($this->array[$offset]); } public function offsetGet($offset) { return isset($this->array[$offset]) ? $this->array[$offset] : $this->default ; } public function offsetSet($offset, $value) { $this->array[$offset] = $value; } public function offsetUnset($offset) { unset($this->array[$offset]); } }
Usage:
$array = array_fill_keys(range('A', 'C'), 'value'); $array = new PigArray($array, 'default'); $a = $array['A']; # string(13) "value" $idx = $array['IDX']; # NULL "default" var_dump($a, $idx);
Demo: https://eval.in/80896
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With