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: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: brad at njoe dot com Assigned:
Status: Open Package: Arrays related
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: brad at njoe dot com
New email:
PHP Version: OS:

 

 [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-2019 The PHP Group
All rights reserved.
Last updated: Tue Nov 12 20:01:33 2019 UTC