php / php/php-src

`array_push($arr, ...$map)` should support non-numeric keys

未关闭
#11,026 12 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Category: Arrays Extension: standard Feature Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.2k
平均合并
2 天 15 小时
30 天内合并 PR
103

描述

Description

The following code:

https://3v4l.org/0p5Sv

<?php

function array_push_cust(&$arr, ...$args) {
    foreach ($args as $k => $v) {
        if (is_int($k)) {
            $arr[] = $v;
        } else {
            $arr[$k] = $v;
        }
    }
}

$arr = [1, 'a' => 2];
$arr2 = [3, 'b' => 4];

$res = $arr;
array_push_cust($res, ...$arr2);
print_r($res);

$res = $arr;
array_push($res, ...$arr2);
print_r($res);

Resulted in this output:

Array
(
    [0] => 1
    [a] => 2
    [1] => 3
    [b] => 4
)

Fatal error: Uncaught ArgumentCountError: array_push() does not accept unknown named parameters in /in/0p5Sv:21
Stack trace:
#0 /in/0p5Sv(21): array_push(Array, 3, b: 4)
#1 {main}
  thrown in /in/0p5Sv on line 21

But I expected this output instead:

Array
(
    [0] => 1
    [a] => 2
    [1] => 3
    [b] => 4
)
Array
(
    [0] => 1
    [a] => 2
    [1] => 3
    [b] => 4
)
PHP Version

PHP 8.0 and higher

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 3v4l 复现开始,确认 PHP 8.0+ 对带有字符串键的解包数组的行为。跟踪 array_push 入口点以及现有的参数处理覆盖范围;完成的标准是,该示例在不出现命名参数错误的情况下,同时保留数字键和非数字键。

由索引模型根据 Issue 内容生成。

评估

技术栈
php
领域
backend
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。