# ArrayPath

- Thread: https://externals.io/message/77803
- Original archive: https://news-web.php.net/php.internals/77803
- Started by Mathias Grimm on 2014-10-07T16:05:15+00:00
- Messages: 13
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #77803 — Mathias Grimm — 2014-10-07T16:05:15+00:00

URL: https://externals.io/message/77803#77803

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');


Regards,
Mathias

---

## #77804 — Florian Margaine — 2014-10-07T16:12:59+00:00

URL: https://externals.io/message/77803#77804 · In reply to: #77803

Hi!

How is it better than:

$idx3 = $_POST['idx1']['idx2']['idx3'];

?

Regards,
Florian Margaine
Le 7 oct. 2014 18:05, "Mathias Grimm" <mathiasgrimm@gmail.com> a écrit :

> 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');
>
>
> Regards,
> Mathias

---

## #77805 — Mathias Grimm — 2014-10-07T16:14:37+00:00

URL: https://externals.io/message/77803#77805 · In reply to: #77804

default value and existence of index checking.

I not a huge change, its a good change and very handy.

<?php$post = array(
    'user' => array(
        'basicInformation' => array(
            'name'    => 'Mathias',
            'surname' => 'Grimm'
        ),
    ));
// normal php way$sName    =
isset($post['user']['basicInformation']['name'   ]) ?
$post['user']['basicInformation']['name'   ] : null;$sSurname =
isset($post['user']['basicInformation']['surname']) ?
$post['user']['basicInformation']['surname'] : null;
// default value$sLocale = isset($post['user']['locale']) ?
$post['user']['locale'] : 'Europe/Dublin';
// ===================================================================
// ArrayPath$sName    = ArrayPath::get('user/basicInformation/name'
, $post);$sSurname = ArrayPath::get('user/basicInformation/surname' ,
$post);
// with default value$sLocale  = ArrayPath::get('user/locale', $post,
'Europe/Dublin');



On Tue, Oct 7, 2014 at 5:12 PM, Florian Margaine <florian@margaine.com>
wrote:

> Hi!
>
> How is it better than:
>
> $idx3 = $_POST['idx1']['idx2']['idx3'];
>
> ?
>
> Regards,
> Florian Margaine
> Le 7 oct. 2014 18:05, "Mathias Grimm" <mathiasgrimm@gmail.com> a écrit :
>
> Hi,
> > […]

---

## #77806 — Mathias Grimm — 2014-10-07T16:19:11+00:00

URL: https://externals.io/message/77803#77806 · In reply to: #77805

This is handy just for programmers using recommended php.ini settings for
development (error_reporting = `E_ALL` and display_errors = On).

People using @, display_errors = Off or any other sort of sorcery don't
have such problems.

On Tue, Oct 7, 2014 at 5:14 PM, Mathias Grimm <mathiasgrimm@gmail.com>
wrote:

> default value and existence of index checking.
>
> I not a huge change, its a good change and very handy.
>
> <?php$post = array(
>     'user' => array(
>         'basicInformation' => array(
>             'name'    => 'Mathias',
>             'surname' => 'Grimm'
>         ),
>     ));
> // normal php way$sName    = isset($post['user']['basicInformation']['name'   ]) ? $post['user']['basicInformation']['name'   ] : null;$sSurname = isset($post['user']['basicInformation']['surname']) ? $post['user']['basicInformation']['surname'] : null;
> // default value$sLocale = isset($post['user']['locale']) ? $post['user']['locale'] : 'Europe/Dublin';
> // ===================================================================
> // ArrayPath$sName    = ArrayPath::get('user/basicInformation/name'    , $post);$sSurname = ArrayPath::get('user/basicInformation/surname' , $post);
> // with default value$sLocale  = ArrayPath::get('user/locale', $post, 'Europe/Dublin');
>
>
>
> On Tue, Oct 7, 2014 at 5:12 PM, Florian Margaine <florian@margaine.com>
> wrote:
>
> > […]

---

## #77807 — Matthew Fonda — 2014-10-07T16:21:45+00:00

URL: https://externals.io/message/77803#77807 · In reply to: #77803

On Tue, Oct 7, 2014 at 9:05 AM, Mathias Grimm <mathiasgrimm@gmail.com>
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');
>
>
> Regards,
> Mathias
>

Hi Mathias,

The new null coalesce operator in PHP 7 achieves what you're looking for
here. See https://wiki.php.net/rfc/isset_ternary

Best regards,
--Matthew

---

## #77808 — Mathias Grimm — 2014-10-07T16:36:07+00:00

URL: https://externals.io/message/77803#77808 · In reply to: #77807

Great! That solves 75% of the problem.

Still accessing index using "/" (or other separator) would be very handy

$c = $_POST::path('a/b/c', 'def')
$c = $_POST::('a/b/c', 'def')
$c = $_POST@('a/b/c', 'def')
$c = $_POST::at('a/b/c', 'def')


I am not really creative now, but the part I would like to highlight is the
feature and not really the syntax.






> On Tue, Oct 7, 2014 at 9:05 AM, Mathias Grimm <mathiasgrimm@gmail.com>
> wrote:
>
> > […]
>
> Hi Mathias,
>
> The new null coalesce operator in PHP 7 achieves what you're looking for
> here. See https://wiki.php.net/rfc/isset_ternary
>
> Best regards,
> --Matthew

---

## #77809 — Mathias Grimm — 2014-10-07T16:42:00+00:00

URL: https://externals.io/message/77803#77809 · In reply to: #77808

For international user this makes more sense because the "'" or the '"' are
sensitive keys that needs two interactions instead of just one...
so for typing something like $_POST['asasdasd']['asdasd']['asdasd'] is
sometimes a bit annoying but it could be solved having 2 keyboard layouts
(my case)

As I said before, this is not something that is going to change the php
direction, however many developer would get very happy.
I have never seen this in other languages but my knowledge is not very
expressive as well.

On Tue, Oct 7, 2014 at 5:36 PM, Mathias Grimm <mathiasgrimm@gmail.com>
wrote:

> Great! That solves 75% of the problem.
>
> Still accessing index using "/" (or other separator) would be very handy
>
> $c = $_POST::path('a/b/c', 'def')
> $c = $_POST::('a/b/c', 'def')
> $c = $_POST@('a/b/c', 'def')
> $c = $_POST::at('a/b/c', 'def')
>
>
> I am not really creative now, but the part I would like to highlight is
> the feature and not really the syntax.
>
>
>
>
> 
>
> > […]

---

## #77810 — johannes@schlueters.de — 2014-10-07T17:15:56+00:00

URL: https://externals.io/message/77803#77810 · In reply to: #77803

> 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.

johannes

---

## #77811 — Mathias Grimm — 2014-10-07T17:22:16+00:00

URL: https://externals.io/message/77803#77811 · In reply to: #77810

That would cause some problems indeed. But you could scape as in other
strings.
at('a/\/b') not nice but we have the same for every string

On Tuesday, October 7, 2014, Johannes Schlüter <johannes@schlueters.de>
wrote:

> 
> > […]
>
> 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.
>
> johannes

---

## #77812 — Ingwie Phoenix — 2014-10-07T17:30:11+00:00

URL: https://externals.io/message/77803#77812 · In reply to: #77803

Am 07.10.2014 um 18:05 schrieb Mathias Grimm <mathiasgrimm@gmail.com>:

> 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');
> 
> 
> Regards,
> Mathias

Oh my god. This is seriously one enhancement that I haven’t seen before. If a function like this makes it into PHP, we can speak of one very unique enhanement here. I like the example of a later email, as in

    $_POST::get(…)

Extending the capabilities of arrays and giving them something like above, would be nice. But I can see where the syntax would clash: $_POST would be expected to be an object with a __toString() method, :: refferences to a static method… 

But maybe, some smart head can figure something out. It would, at least, be an interesting functionality.

By the way, I would suggest swapping the path and array argument. As in, „Get from $array the path ‚…‘“. Just a suggestion.

---

## #77817 — Mike Willbanks — 2014-10-07T18:18:10+00:00

URL: https://externals.io/message/77803#77817 · In reply to: #77810

On Tue, Oct 7, 2014 at 12:15 PM, Johannes Schlüter <johannes@schlueters.de>
wrote:

> 
> > […]
>
> 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

---

## #77822 — Korvin Szanto — 2014-10-07T23:48:06+00:00

URL: https://externals.io/message/77803#77822 · In reply to: #77817

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 12:15 PM, Johannes Schlüter <johannes@schlueters.de>
> wrote:
>
> > […]
>
>
> 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.
>
>
>
> > […]

---

## #77825 — Michael Dowling — 2014-10-08T01:21:48+00:00

URL: https://externals.io/message/77803#77825 · In reply to: #77817

> 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.


I agree that this definitely does not belong in core.

This has been tried with json via json-path, but there's no formal grammar or compliance test suite to back it. It's basically a really old blog post that has never been formalized. There's a much more robust alternative called jmespath that works across multiple languages and has a PHP implementation: https://github.com/mtdowling/jmespath.php

I think this shows that this is a problem that can and has already been solved in user land (multiple times in multiple ways), and it can be done in a performant way (e.g., compiling expressions into PHP code). I don't see a need to add bloat to PHP's core for this.

-Michael
