Req #64818 [Com]: array_get

From: Date: Mon, 21 Oct 2013 14:01:48 +0000
Subject: Req #64818 [Com]: array_get
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-182361@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64818&edit=1 ID: 64818 Comment by: john dot papaioannou at gmail dot com Reported by: mario dot dweller at seznam dot cz Summary: array_get Status: Wont fix Type: Feature/Change Request Package: Arrays related PHP Version: Irrelevant Block user comment: N Private report: N New Comment: It's 4Q2013 and apparently the attitude of internals as a whole is still as broken as ever. "Zero value added"? Sure, if you say so. I guess the people who are interested in accessing nested values in scenarios where hardcoding the expected structure of the array is impossible (e.g. "key1.key2.key3" is only known at runtime) do not exist, because TO YOU this looks like "zero value". I am not even going to bother linking to StackOverflow where people have repeatedly asked ***how to write a function*** that does this, or to components such as Laravel's Container that implement this in userland. Keep calm and let us continue guessing if the sign of the zero f***s you give is positive or negative. Previous Comments: ------------------------------------------------------------------------ [2013-05-12 11:31:33] mario dot dweller at seznam dot cz And if null is considered as a valid value and the value is deep inside the array... $d = isset($array['a']['b']['c']) && array_key_exists('d', $array['a']['b']['c']) ? $array['a']['b']['c']['d'] : 'default'; or just array_get($array, ['a', 'b', 'c', 'd'], 'default'); ------------------------------------------------------------------------ [2013-05-12 11:26:01] mario dot dweller at seznam dot cz In the case you have those keys in a variable like $keys = ['key1', 'key2'] you can access it only using a loop. That's one of the purpose of the function. BTW what looks better and what is more readable? $a = isset($array['a']) ? $array['a'] : null; $b = array_key_exists('b', $array) ? $array['b'] : 'default'; or $a = array_get($array, 'a'); $b = array_get($array, 'b', 'default'); ? ------------------------------------------------------------------------ [2013-05-12 11:13:28] pajoye@php.net @niki Arg right. But still should not be added, zero value added here. ------------------------------------------------------------------------ [2013-05-12 10:56:45] nikic@php.net @Pierre: isset($array['key1']['key2']) ?: NULL is the same as isset($array['key1']['key2']) ? isset($array['key1']['key2']) : NULL is the same as "true" if the key exists, "null" if it doesn't. That's not what this req is about. ------------------------------------------------------------------------ [2013-05-12 10:53:39] pajoye@php.net $val = isset($array['key1']['key2']) ?: NULL; We already have an operator for that ($v = (iftrue) ?: defaultvalue). Operators are also much more efficient than a function call. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=64818 -- Edit this bug report at https://bugs.php.net/bug.php?id=64818&edit=1

« previous php.bugs (#182361) next »