php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40792 Wish: Function array_get(&$mixed, $key, $defaultvalue)
Submitted: 2007-03-13 11:23 UTC Modified: 2015-01-09 00:43 UTC
Votes:13
Avg. Score:4.8 ± 0.4
Reproduced:12 of 12 (100.0%)
Same Version:6 (50.0%)
Same OS:6 (50.0%)
From: t dot kloppenburg at billiton dot de Assigned: ajf (profile)
Status: Closed Package: *General Issues
PHP Version: 5.2.1 OS: Linux
Private report: No CVE-ID: None
 [2007-03-13 11:23 UTC] 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.



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-13 14:35 UTC] t dot kloppenburg at billiton dot de
ok, I code in PHP all this years and I miss it in PHP :)
 [2007-09-11 16:50 UTC] 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.diff
http://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
 [2010-10-13 10:47 UTC] 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.
 [2015-01-09 00:43 UTC] ajf@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: ajf
 [2015-01-09 00:43 UTC] ajf@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC