Re: ArrayPath

From: Date: Tue, 07 Oct 2014 23:48:06 +0000
Subject: Re: ArrayPath
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-77822@lists.php.net to get a copy of this message
In every project I have worked on in the last few years, I've implemented this. If I have an array that I can't be sure is well formed and need to get a value at any depth without triggering expensive notices, the recommended way today is to check if each value is set and is an array: http://3v4l.org/23c9i With this functionality, I can simply do: array_get('key1.key2', 'default') On Tue, Oct 7, 2014 at 11:18 AM, Mike Willbanks <pencap@gmail.com> wrote: > On Tue, Oct 7, 2014 at 12:15 PM, Johannes Schlüter <johannes@schlueters.de> > wrote: > >> On Tue, 2014-10-07 at 17:05 +0100, Mathias Grimm wrote: >> > Hi, >> > I would like to suggest something for php like a class I am using >> > >> > https://github.com/mathiasgrimm/arraypath >> > >> > The reason is to access arrays like this: >> > >> > $idx3 = ArrayPath::get('idx1/idx2/idx3', $_POST, >> > 'myDefaultValue'); >> >> what hppens if my array has a / as part of the key? Do I have to check >> that beforehand and set a custom identifier? So what happens if I use >> your example with $_GET instead of $_POST and a user requests >> foo.php?idx1=idx2/idx3 >> >> I'm not really convinced. >> > > > This request seems to be more in the realm of having XPath type support for > arrays, likewise people have also attempted to do this with JSON. It does > seem like it could be valuable to some degree, however, I don't necessarily > believe it belongs in the core. This seems much better for a PECL module > if necessary. > > If the key has a / it would seem like you should escape it like a regular > string escape, aka "\/". I'm not convinced this is something that is > necessary in PHP either. There does not seem to be a great use case for > this use. > > > >> >> johannes >> >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >>

« previous php.internals (#77822) next »