php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64818 array_get
Submitted: 2013-05-11 17:07 UTC Modified: 2013-05-12 11:13 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:3 (60.0%)
From: mario dot dweller at seznam dot cz Assigned:
Status: Wont fix Package: Arrays related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mario dot dweller at seznam dot cz
New email:
PHP Version: OS:

 

 [2013-05-11 17:07 UTC] mario dot dweller at seznam dot cz
Description:
------------
What about to create a new array function array_get? When we have a function like array_column...

function array_get(array $array, $key, $default = null)

Something like this.
http://api.nette.org/2.0/source-Utils.Arrays.php.html#36-53

Ofc i can use that Arrays::get method or i can create my own function/static method. But it has a performance overhead for quite a common thing which should be as fast as it's possible.

I hate typing this. 

isset($array['key1']['key2']) ? $array['key1']['key2'] : null;

This looks better.

array_get($array, ['key1', 'key2'], null)

or just

array_get($array, 'key')


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-12 06:41 UTC] fl at nyggen dot com
You could just write:

@$array['key1']['key2'];

Which will give you the same result.

@$array['key1']['key2'] ?: 'Key not found'
 [2013-05-12 10:37 UTC] mario dot dweller at seznam dot cz
fl at nyggen dot com: I know this is possible. But i don't like suppressing warning in this situation. And 

@$array['key1']['key2'] ?: 'Key not found'

And the valid value could be false/0/""/"0"/[].
 [2013-05-12 10:53 UTC] 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.
 [2013-05-12 10:53 UTC] pajoye@php.net
-Status: Open +Status: Wont fix
 [2013-05-12 10:56 UTC] 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 11:13 UTC] pajoye@php.net
@niki

Arg right. But still should not be added, zero value added here.
 [2013-05-12 11:26 UTC] 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:31 UTC] 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-10-21 14:01 UTC] john dot papaioannou at gmail dot com
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.
 [2013-10-27 17:39 UTC] j dot zuckerman+php at gmail dot com
I don't know whether it's really appropriate for the framework to define this function.. surely it could be done faster in C but I'm sure this is fast enough:

if (function_exists('array_get') === false) {
    function array_get($array, $key, $default) {
        return isset($array[$key]) ? $array[$key] : $default;
    }
}
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Apr 10 14:01:24 2021 UTC