Skip to content

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

Open
@mvorisek

Description

@mvorisek

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions