{"id":1229,"date":"2022-08-30T15:14:32","date_gmt":"2022-08-30T15:14:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/safely-get-array-element-value-for-defined-and-undefined-indexes-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:32","modified_gmt":"2022-08-30T15:14:32","slug":"safely-get-array-element-value-for-defined-and-undefined-indexes-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/safely-get-array-element-value-for-defined-and-undefined-indexes-collection-of-common-programming-errors\/","title":{"rendered":"safely get array element value for defined and undefined indexes-Collection of common programming errors"},"content":{"rendered":"<p>As far as you only need NULL as &#8220;default&#8221; value, you can make use of the error suppression operator:<\/p>\n<pre><code>$x = @$array['idx'];\n<\/code><\/pre>\n<p>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 <code>ArrayAccess<\/code> interface:<\/p>\n<pre><code>class PigArray implements ArrayAccess\n{\n    private $array;\n    private $default;\n\n    public function __construct(array $array, $default = NULL)\n    {\n    $this-&gt;array   = $array;\n    $this-&gt;default = $default;\n    }\n\n    public function offsetExists($offset)\n    {\n    return isset($this-&gt;array[$offset]);\n    }\n\n    public function offsetGet($offset)\n    {\n    return isset($this-&gt;array[$offset]) ? $this-&gt;array[$offset] : NULL;\n    }\n\n    public function offsetSet($offset, $value)\n    {\n    $this-&gt;array[$offset] = $value;\n    }\n\n    public function offsetUnset($offset)\n    {\n    unset($this-&gt;array[$offset]);\n    }\n}\n<\/code><\/pre>\n<p>Usage:<\/p>\n<pre><code>$array = array_fill_keys(range('A', 'C'), 'default value');\n$array = new PigArray($arrayData);\n$a     = $array['A'];   # string(13) \"default value\"\n$idx   = $array['idx']; # NULL\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:00. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>As far as you only need NULL as &#8220;default&#8221; value, you can make use of the error suppression operator: $x = @$array[&#8216;idx&#8217;]; 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 { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1229","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1229"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1229\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}