php / php/php-src

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

Open
#11,026 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Arrays Extension: standard Feature Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked 3v4l reproduction and confirm the PHP 8.0+ behavior for unpacked arrays with string keys. Trace the array_push entry point and existing coverage for argument handling; done means the example preserves both numeric and non-numeric keys without the named-parameter error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.