Edit report at https://bugs.php.net/bug.php?id=64818&edit=1

 ID:                 64818
 Updated by:         paj...@php.net
 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:

@niki

Arg right. But still should not be added, zero value added here.


Previous Comments:
------------------------------------------------------------------------
[2013-05-12 10:56:45] ni...@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] paj...@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:37:02] 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 06:41:16] 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-11 17:07:24] 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')



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64818&edit=1

Reply via email to