PHP
Extract and Remap Columns from Array of Objects/Arrays
Efficiently extract a specific column from a multi-dimensional PHP array or array of objects, optionally remapping its key to create a new flat associative array.
<?php
/**
* Extracts a column from an array of arrays/objects and optionally renames the key.
*
* @param array $array The input array of arrays or objects.
* @param string $columnKey The key or property to extract.
* @param string|null $indexKey (Optional) The key to use for the new array's keys. If null, a simple list is returned.
* @param string|null $newColumnKey (Optional) The new key name for the extracted column in the resulting associative array.
* @return array The extracted column as a new array.
*/
function array_column_remapped(array $array, string $columnKey, ?string $indexKey = null, ?string $newColumnKey = null): array
{
$result = [];
foreach ($array as $item) {
$value = is_array($item) ? ($item[$columnKey] ?? null) : ($item->$columnKey ?? null);
$index = $indexKey ? (is_array($item) ? ($item[$indexKey] ?? null) : ($item->$indexKey ?? null)) : null;
if ($newColumnKey) {
$entry = [$newColumnKey => $value];
} else {
$entry = $value;
}
if ($index !== null) {
$result[$index] = $entry;
} else {
$result[] = $entry;
}
}
return $result;
}
// Example usage with arrays:
$users = [
['id' => 1, 'name' => 'Alice', 'email' => '[email protected]'],
['id' => 2, 'name' => 'Bob', 'email' => '[email protected]'],
['id' => 3, 'name' => 'Charlie', 'email' => '[email protected]']
];
// Extract 'email' as a simple list
$emails = array_column_remapped($users, 'email');
// Expected: ['[email protected]', '[email protected]', '[email protected]']
print_r($emails);
// Extract 'name' using 'id' as keys
$userNamesById = array_column_remapped($users, 'name', 'id');
// Expected: [1 => 'Alice', 2 => 'Bob', 3 => 'Charlie']
print_r($userNamesById);
// Extract 'email' and rename it to 'user_email', indexed by 'id'
$userEmailsRenamed = array_column_remapped($users, 'email', 'id', 'user_email');
// Expected: [1 => ['user_email' => '[email protected]'], 2 => ['user_email' => '[email protected]'], 3 => ['user_email' => '[email protected]']]
print_r($userEmailsRenamed);
// Example usage with objects:
class User { public $id; public $name; public $email; public function __construct($id, $name, $email) { $this->id = $id; $this->name = $name; $this->email = $email; } }
$userObjects = [
new User(4, 'David', '[email protected]'),
new User(5, 'Eve', '[email protected]')
];
$objectEmails = array_column_remapped($userObjects, 'email', 'id', 'email_address');
// Expected: [4 => ['email_address' => '[email protected]'], 5 => ['email_address' => '[email protected]']]
print_r($objectEmails);
?>
How it works: This enhanced `array_column_remapped` function provides more flexibility than the native `array_column`. It allows extracting a specific column from an array of arrays or objects, optionally using another column's value as the key for the new array, and can even rename the extracted column's key in the resulting associative array. This is incredibly useful for transforming complex data structures into more specific, usable formats for display or further processing.