Codectionary / Developer documentation / PHP

Select Named Fields with array_intersect_key

Keep entries with allowed keys without confusing field selection with validation or authorisation.

What it is and why to use it

A profile formatter may need only a display name and preferred language from a larger record. array_intersect_key() selects entries from its first array whose keys also occur in every comparison array. The values in the comparison arrays are irrelevant; their keys describe which fields to keep.

How it works

The function preserves the first array’s values and keys. A convenient allowed-key map can be built with array_fill_keys(), which turns a list of names into keys. This is a structural selection step. It does not verify that an email address is valid, a value has the correct type, or a user has permission to change a selected field.

Common mistakes and limitations

Do not pass a plain list of allowed names directly as the second argument: its keys would be 0, 1 and so on. Build a keyed map first. Never treat the selected output as safe for SQL or HTML without the appropriate parameter binding or escaping.

Run the examples locally

Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.

The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.

These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.

Check your interpreter, lint and run one saved example

php -v
php -l example.php
php example.php

Continue learning

Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.

Syntax

$selected = array_intersect_key($record, array_fill_keys($allowedNames, true));

Examples

Build a small public profile record

Select display fields from a record that also contains internal data.

<?php
declare(strict_types=1);

$record = ['name' => 'John', 'language' => 'PHP', 'internalId' => 42];
$allowed = array_fill_keys(['name', 'language'], true);
$public = array_intersect_key($record, $allowed);
echo json_encode($public, JSON_THROW_ON_ERROR), PHP_EOL;
  1. array_fill_keys creates a map with name and language as string keys.
  2. array_intersect_key keeps those matching entries from record and ignores the boolean values in allowed.
  3. The output contains no internalId. This is useful for an explicit response shape, but it is still the application’s responsibility to decide which fields may be public.
  4. Expected output: {"name":"John","language":"PHP"}

Observe that selection does not validate values

A selected email can still fail a separate validation check.

<?php
declare(strict_types=1);

$input = ['email' => 'not-an-email', 'role' => 'admin'];
$selected = array_intersect_key($input, ['email' => true]);
$email = filter_var($selected['email'], FILTER_VALIDATE_EMAIL);
echo $email === false ? 'Invalid email' : 'Valid email', PHP_EOL;
  1. Only email is selected because role is absent from the comparison map.
  2. filter_var applies an explicit email validation filter to the remaining value.
  3. The strict false check detects failure. Keeping a field by name never establishes that its value is valid or authorised.
  4. Expected output: Invalid email

Best practices

  • Define editable or displayable fields explicitly.
  • Validate values and check permissions after selecting fields.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading