← Back to all snippets
PHP

Extract a Specific Column from an Array of Arrays

Easily extract values from a specific column across all rows in a multi-dimensional PHP array using array_column(). Perfect for transforming data or creating lists.

<?php
$users = [
    ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Bob', 'email' => '[email protected]'],
    ['id' => 3, 'name' => 'Charlie', 'email' => '[email protected]'],
];

$names = array_column($users, 'name');
$idsAsKeys = array_column($users, 'name', 'id'); // Extract 'name' using 'id' as keys

echo "User Names: ";
print_r($names);

echo "User Names (ID as Keys): ";
print_r($idsAsKeys);
?>
How it works: The array_column() function returns the values from a single column in the input array. It's particularly useful when you have an array of associative arrays (like a result set from a database) and you need to get a list of all values for a specific key. An optional third argument allows you to use another column's values as the keys for the new array.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs