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: 2021-11-18 15:42 UTC
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: Suspended Package: Arrays related
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
 [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

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-18 15:42 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-11-18 15:42 UTC] cmb@php.net
Such new syntax clearly needs an RFC.  Anybody is welcome to
pursue the RFC process[1].  For the time being, I suspend this
ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC