php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61727 Extend array assignment operator ('=>')
Submitted: 2012-04-13 18:54 UTC Modified: -
From: brad at njoe dot com Assigned:
Status: Open Package: Arrays related
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-04-13 18:54 UTC] brad at njoe dot com
Description:
------------
This feature would allow one to define a value to be used for creating multiple 
array indexes (think of it as an array "fill" with a tad more flexibility). The 
"flexibility" is that rather than using a numerical starting index value and a 
length parameter, this new syntax would set the same value for each value in a 
1-dimensional array of scalars (used as keys).

This syntax is essentially a more convenient/concise way of doing something 
like:

foreach($array_of_keys as $key)
    $the_array[$key] = $value_for_keys;

with the added bonus that it would work both when defining an entire array or 
just 
modifying an existing array. These two use case scenarios are modeled below as 
Use 
Case #1 and #2, respectively.

Test script:
---------------
<?php
/* ---------------------- Use Case #1 ---------------------- */
// set/clear appropriate bits at intialization/definition
$binary = array(
    range( 0,  0) => 1,
    range( 1,  1) => 0,
    range( 2,  3) => 1,
    range( 4, 11) => 0,
    range(12, 15) => 1
);

/* ---------------------- Use Case #2 ---------------------- */
// initialize array representation of 16-bit binary number to 0
$binary = array_fill(0, 16, 0);

// set appropriate bits
$binary[range( 0,  0)] = 1;
$binary[range( 2,  3)] = 1;
$binary[range(12, 15)] = 0;
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 18:01:53 2014 UTC