Req #40792 [Com]: Wish: Function array_get(&$mixed, $key, $defaultvalue)

From: Date: Wed, 13 Oct 2010 08:47:46 +0000
Subject: Req #40792 [Com]: Wish: Function array_get(&$mixed, $key, $defaultvalue)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-155140@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=40792&edit=1 ID: 40792 Comment by: lethalman88 at gmail dot com Reported by: t dot kloppenburg at billiton dot de Summary: Wish: Function array_get(&$mixed, $key, $defaultvalue) Status: Open Type: Feature/Change Request Package: Feature/Change Request Operating System: Linux PHP Version: 5.2.1 Block user comment: N New Comment: I suggest an isset() like function, so that you can array_get($arr[$key], $default), that is at language level. Previous Comments: ------------------------------------------------------------------------ [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.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 ------------------------------------------------------------------------ [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 http://bugs.php.net/bug.php?id=40792&edit=1

« previous php.bugs (#155140) next »