Bug #3928: shorthand notation for filling associative arrays, please

From: Date: Sun, 26 Mar 2000 07:11:31 +0000
Subject: Bug #3928: shorthand notation for filling associative arrays, please
Groups: php.dev 
Request: Send a blank email to php-dev+get-17238@lists.php.net to get a copy of this message
From: erw.dongen@wxs.nl Operating system: Windows 98 PHP version: 3.0.14 PHP Bug Type: Feature/Change Request Bug description: shorthand notation for filling associative arrays, please To my knowledge there's no way of labelling/declaring the keys in an associative arrays once and then simply only specifying the individual values. So when I set up an array with basically constant string values I ended up typing something like this 75 times: $tokens[] = array ( "startcode" => "la", "endcode" => "di", "classname" => "da" ); $tokens[] = array ( "startcode" => "ob", "endcode" => "la", "classname" => "di" ); etc. So an extension to the array() definition would greatly be appreciated to alleviate the typing. I suggest something like: array ( [keylist][keylist][keylist] = val1, val2, val3, etc, ); or in the above example array ( [][keylist] = ) Where 'keylist' is a finite list of keys of the associative array and '[]' denotes the current behaviour of defaulting to numeric keys starting at 'number of element in array =1' Example: $tokens = array ( ['foo', 'bar']['start','end','class'] = "la", "di", "da", // the elements of 'foo' "ob", "la", "di" // ditto 'bar' ); Which gives $tokens['foo']['start']="la", $tokens['foo']['end']="di", $tokens['foo']['class']="da" $tokens['bar']['class']= "ob", $tokens['bar']['class']= "la", $tokens['bar']['class']= "di". Similar: $tokens = array( []['start,'end',class'] = "la", "di","da", "ob", "la", "di" ); gives $tokens[0]['start']="la" etc. until $tokens[1]['class']="di" (assuming $tokens was empty) Possible extensions for numeric keys may be: '[1..3]' denoting a finite list with numeric keys starting with 1 and ending with 3 inclusive. In this case, if less than 3 rows are subsequently defined, row3 would still exist but be empty and '[5..]' denoting 'start at numeric key 5 and count upwards' Basically it all boils down to the inability to specify an associative key-name for a specific index position - a kind of a reverse 'key()' function. (Yes indeed that's the second feature request in the same report;-)) Can't say that I've succeeded in limiting the typing this time, but I hope you can do something with these suggestions. Thanks and keep up the excellent work! Erwin van Dongen

« previous php.dev (#17238) next »