php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #3928 shorthand notation for filling associative arrays, please
Submitted: 2000-03-26 02:11 UTC Modified: 2006-04-03 09:28 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: erw dot dongen at wxs dot nl Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.0 OS: Windows 98
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2000-03-26 02:11 UTC] erw dot dongen at wxs dot nl
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-10 14:15 UTC] jimw@php.net
refiling against 4.0.
 [2003-07-17 16:15 UTC] tomer at parity-bit dot com
I think you could do something with the range() function here as a workaround.
 [2006-04-03 09:28 UTC] tony2001@php.net
This has been discussed several times and the answer is "no".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 05:01:29 2024 UTC