Skip to content

Stdlib: array_slice() — fourth argument $preserve_keys (ext/standard/array.c parity) #4227

Description

@PurHur

Category

stdlib

Status (expanded 2026-06-04 — implementation-ready)

Fourth-argument $preserve_keys is accepted by Zend but rejected at runtime in this compiler. Referenced repro path test/repro-maintainer/array_slice_preserve_keys.php was never committed — inline repro below.

Problem

array_slice($array, $offset, $length, $preserve_keys) accepts a fourth boolean argument in PHP. When true, returned slice must keep original keys (e.g. ['c'=>2,'d'=>3] from the tail). This compiler rejects four arguments at runtime.

php-src reference

Repro (failure today)

Save as repro_array_slice_preserve_keys.php:

<?php
$a = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
$r = array_slice($a, 2, 2, true);
echo implode(',', array_keys($r)), '|', implode(',', $r), "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php repro_array_slice_preserve_keys.php
php bin/vm.php repro_array_slice_preserve_keys.php
'
Runtime Output
Zend PHP 8.x c,d|2,3
bin/vm.php LogicException: array_slice() requires two or three arguments in this compiler build

Three-argument form must remain unchanged (reindex list keys when $preserve_keys omitted/false).

Scope (this repo)

Layer Path Notes
VM ext/standard/array_slice.php, VmArray helper Parse 4th arg; copy keys when true
JIT lib/JIT/Builtin/Array*.php or JitArraySlice Optional 4th arg lowering
AOT fixture under test/fixtures/aot/cases/
Tests test/compliance/cases/stdlib/array_slice_preserve_keys.phpt

Done when

  • Repro prints c,d|2,3 on VM (Docker php-compiler:22.04-dev)
  • Three-arg array_slice($a, 0, 2) on list still returns 0,1 keys
  • ./script/ci-fast.sh --filter array_slice_preserve green

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter array_slice'

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions