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!
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 usinghttps://github.com/mathiasgrimm/arraypath
The reason is to access arrays like this:
$idx3 = ArrayPath::get('idx1/idx2/idx3', $_POST, 'myDefaultValue');
Regards,
Mathias
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,
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
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: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
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 usinghttps://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
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,
I would like to suggest something for php like a class I am usinghttps://github.com/mathiasgrimm/arraypath
The reason is to access arrays like this:
$idx3 = ArrayPath::get('idx1/idx2/idx3', $_POST, 'myDefaultValue');
Regards,
MathiasHi Mathias,
The new null coalesce operator in PHP 7 achieves what you're looking for
here. See https://wiki.php.net/rfc/isset_ternaryBest regards,
--Matthew
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.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 usinghttps://github.com/mathiasgrimm/arraypath
The reason is to access arrays like this:
$idx3 = ArrayPath::get('idx1/idx2/idx3', $_POST, 'myDefaultValue');
Regards,
MathiasHi Mathias,
The new null coalesce operator in PHP 7 achieves what you're looking for
here. See https://wiki.php.net/rfc/isset_ternaryBest regards,
--Matthew
Hi,
I would like to suggest something for php like a class I am usinghttps://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
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:
Hi,
I would like to suggest something for php like a class I am usinghttps://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/idx3I'm not really convinced.
johannes
On Tue, Oct 7, 2014 at 12:15 PM, Johannes Schlüter johannes@schlueters.de
wrote:
Hi,
I would like to suggest something for php like a class I am usinghttps://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/idx3I'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
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:Hi,
I would like to suggest something for php like a class I am usinghttps://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/idx3I'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
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
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 usinghttps://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.