array_column, array_flip, array_combine
Concept
Three functions for reshaping arrays: array_column extracts a column from a multi-dimensional array, array_flip swaps keys and values, and array_combine builds an array from separate key and value arrays.
array_column($input, $column_key, $index_key) extracts all values for a given key from a list of records. Optional $index_key lets you specify which field to use as the index of the result. This replaces a common foreach pattern with a single function call. Works with arrays of arrays and arrays of objects. Pass null as $column_key to reindex the original records by $index_key without extracting a single column.
array_flip($array) returns an array with keys and values exchanged. Values become keys, keys become values. Values must be valid keys (strings or integers); duplicate values result in the last key winning. Common use: convert a sequential list of values into a lookup hash for O(1) isset checks instead of O(n) in_array checks.
array_combine($keys, $values) creates an array using one array as keys and another as values. Both arrays must have the same number of elements (throws ValueError otherwise). Useful when you have data split across two parallel arrays.
Code Example
<?php
declare(strict_types=1);
// array_column — extract field from records
$users = [
['id' => 1, 'name' => 'Alice', 'role' => 'admin'],
['id' => 2, 'name' => 'Bob', 'role' => 'editor'],
['id' => 3, 'name' => 'Carol', 'role' => 'admin'],
];
// Extract all names
$names = array_column($users, 'name');
// ['Alice', 'Bob', 'Carol']
// Extract names, indexed by ID
$nameById = array_column($users, 'name', 'id');
// [1 => 'Alice', 2 => 'Bob', 3 => 'Carol']
// Reindex whole records by ID (column_key = null)
$usersById = array_column($users, null, 'id');
// [1 => [...], 2 => [...], 3 => [...]] — full records keyed by id
// Works with objects too
class User { public function __construct(public int $id, public string $name) {} }
$objects = [new User(1, 'Alice'), new User(2, 'Bob')];
$names = array_column($objects, 'name'); // ['Alice', 'Bob']
// array_flip — swap keys and values
$statusCodes = ['active' => 1, 'inactive' => 2, 'banned' => 3];
$flipped = array_flip($statusCodes);
// [1 => 'active', 2 => 'inactive', 3 => 'banned']
// Fast lookup: O(1) isset vs O(n) in_array
$allowedRoles = ['admin', 'editor', 'viewer'];
$allowedHash = array_flip($allowedRoles);
// ['admin' => 0, 'editor' => 1, 'viewer' => 2]
// O(1) check
if (isset($allowedHash['admin'])) {
echo "Allowed\n";
}
// vs O(n) check (never do this for large arrays):
if (in_array('admin', $allowedRoles)) {}
// array_combine — zip two arrays into one
$columns = ['id', 'name', 'email'];
$values = [42, 'Alice', 'alice@example.com'];
$record = array_combine($columns, $values);
// ['id' => 42, 'name' => 'Alice', 'email' => 'alice@example.com']
// Practical: map DB column names to a flat result row
$headers = ['first_name', 'last_name', 'age'];
$row = ['Alice', 'Smith', 30];
$mapped = array_combine($headers, $row);