#3928 [Com]: shorthand notation for filling associative arrays, please

From: Date: Thu, 17 Jul 2003 21:15:31 +0000
Subject: #3928 [Com]: shorthand notation for filling associative arrays, please
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-44206@lists.php.net to get a copy of this message
 ID:               3928
 Comment by:       tomer at parity-bit dot com
 Reported By:      erw dot dongen at wxs dot nl
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: Windows 98
 PHP Version:      4.0
 New Comment:

I think you could do something with the range() function here as a
workaround.


Previous Comments:
------------------------------------------------------------------------

[2001-02-10 14:15:33] jimw@php.net

refiling against 4.0.

------------------------------------------------------------------------

[2000-03-26 02:11:31] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=3928&edit=1



Thread (1 message)

  • tomer at parity-bit dot com
  • Unknown Message
    • tomer at parity-bit dot com
« previous php.bugs (#44206) next »