Alex Alex - 5 months ago 14
PHP Question

safely get array element value for defined and undefined indexes

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:


  • disabling
    error_reporting

  • ternary
    isset
    /
    empty
    check

  • error control operator
    @

  • creating my own global function or application accessor/filter



Something like:

$x = get_if_set($array['idx']);
// or
$x = Zend_XXX($array, 'idx')

Answer

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