Edit report at https://bugs.php.net/bug.php?id=40792&edit=1
ID: 40792
Updated by: ajf@php.net
Reported by: t dot kloppenburg at billiton dot de
Summary: Wish: Function array_get(&$mixed, $key,
$defaultvalue)
-Status: Open
+Status: Closed
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Linux
PHP Version: 5.2.1
-Assigned To:
+Assigned To: ajf
Block user comment: N
Private report: N
New Comment:
The fix for this bug has been committed.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
For Windows:
http://windows.php.net/snapshots/
Thank you for the report, and for helping us make PHP better.
This is essentially fixed by the ?? (null coalesce operator) added in PHP 7:
https://wiki.php.net/rfc/isset_ternary
Previous Comments:
------------------------------------------------------------------------
[2010-10-13 10:47:46] lethalman88 at gmail dot com
I suggest an isset() like function, so that you can array_get($arr[$key], $default), that is at
language level.
------------------------------------------------------------------------
[2007-09-11 16:50:56] andrew at ashearer dot com
I submitted a patch implementing array_get() for PHP 6. If the patch
is accepted and there's interest, I could submit the PHP 5 version
also. See the ongoing discussion on the PHP internals mailing list:
[PHP-DEV] [PATCH] array_get()
http://marc.info/?l=php-internals&m=118946242013246&w=2
Patches against HEAD and test suite (PHP 6):
http://ashearer.com/software/array_get/2007-09-10-php6/array_get.diffhttp://ashearer.com/software/array_get/2007-09-10-php6/array_get.phpt
Proposal:
array_get, a more palatable alternative to ifsetor
MOTIVATION
There is an unmet need for an accessor that doesn't generate an
E_NOTICE when the value is missing, as shown by ongoing discussions
and repeated requests for an ifsetor operator. However, ifsetor had a
special-case syntax and generally didn't fit very well with the rest
of the language.
http://devzone.zend.com/node/view/id/1481#Heading2
has a brief
summary. See the Related Functions and Proposals section for more.
Reading over those ideas (firstset(), coalesce(), :?, ifset(), and a
workaround using settype()), most of the best uses boil down to
retrieving values from arrays.
PROPOSAL
As a simpler alternative to constructs such as this common double
array reference...
$value = isset($_POST['command']) ? $_POST['command'] : '';
I propose an array_get function, like this...
$value = array_get($_POST, 'command', '');
The third argument provides a default. This function would require no
special syntax, and makes a very common construct easier to read and
less error-prone to type. It's a concise way of saying that missing
values can be handled gracefully.
Though request processing was used as an example, the function has
wide applicability across many other uses of associative arrays.
For discussion of limitations and alternatives, see the rest of the
proposal at:
http://marc.info/?l=php-internals&m=118946242013246&w=2
------------------------------------------------------------------------
[2007-03-13 14:35:51] t dot kloppenburg at billiton dot de
ok, I code in PHP all this years and I miss it in PHP :)
------------------------------------------------------------------------
[2007-03-13 11:23:03] t dot kloppenburg at billiton dot de
Description:
------------
I miss a function or ArrayObject method to get an element of an array, or alternativly a default
value if the key is not set in the array.
In python:
mydict = {'key1' : 'value1'}
val = mydict.get('otherkey', 'defaultvalue')
-> 'defaultvalue'
This is very handy when dealing with arrays.
I'ld be happy to see this in PHP4 and PHP5. It could look like this:
$cfg = array('version' => '1.2v', 'othercfg' =>
'otherval');
$tmppath = array_get( $cfg, 'tmppath', '/tmp' );
-> '/tmp'
or with ArrayObject as:
$tmppath = $arrobj->get( 'tmppath', '/tmp' );
-> '/tmp' (if not set in the array)
I code in Python since 6 or 7 years, and I really miss this function in everyday-use.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=40792&edit=1