Skip to content
PHP News
Search
Declined PHP 8.6

array_only_keys() and array_except_keys()

Adds array_only_keys() and array_except_keys() to keep or drop the listed keys of an array without the array_flip() workaround.

Add array_only_keys() and array_except_keys()

Primary vote · 2/3 majority

2 Yes 9 No 9 abstain 18% approval

This poll has closed.

The RFC targeted PHP 8.6 and needed a two-thirds majority. It was declined 2 to 9, with 9 abstaining, when voting closed on March 26, 2026.

Summary

Keeping some keys of an array, or dropping a few, is a common task, and many frameworks ship helpers for it, like Laravel's Arr::only() and Arr::except(). This RFC from Muhammed Arshid KV proposed two native functions to do the same job in PHP: array_only_keys() and array_except_keys(). It was declined.

How it would have worked

function array_only_keys(array $array, array $keys): array {}
function array_except_keys(array $array, array $keys): array {}

The first function keeps only the keys you list, and the second keeps everything except the keys you list.

$a = [10, 20, 30, 40];

array_only_keys($a, [1, 3]);
// [1 => 20, 3 => 40]

array_except_keys($a, [0, 2]);
// [1 => 20, 3 => 40]

The RFC set out a few rules:

  • Both functions return a new array and leave the original unchanged.
  • Keys keep their original order.
  • Keys that don't exist are skipped without an error.
  • Duplicate keys in $keys are ignored.
  • Both string and integer keys work.

Why add them

You can already write these in userland:

function array_only_keys(array $input, array $keys): array {
    return array_intersect_key($input, array_flip($keys));
}

That works, but array_flip() builds an extra array in memory first. The RFC argues that a native C version can skip that step and copy values straight into the result, which uses less memory, especially with large key lists or inside loops.

What it means for existing code

According to the RFC, nothing breaks. It only adds two new functions to the standard library.