PHP

Extract a Specific Column from an Array of Arrays or Objects

Learn to quickly extract all values from a specific column in a multi-dimensional PHP array or array of objects, creating a simple flat array with array_column.

<?php
$records = [
    ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Jane Smith', 'email' => '[email protected]'],
    ['id' => 3, 'name' => 'Peter Jones', 'email' => '[email protected]']
];

$names = array_column($records, 'name');
$ids = array_column($records, 'id');

print_r($names);
// Output: Array ( [0] => John Doe [1] => Jane Smith [2] => Peter Jones )

print_r($ids);
// Output: Array ( [0] => 1 [1] => 2 [2] => 3 )
How it works: `array_column()` is an incredibly useful function for extracting a single column from a multi-dimensional array or an array of objects. It returns an array containing the values from a single input column, which is perfect for generating lists of specific attributes from a dataset without needing to loop manually.

Need help integrating this into your project?

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

Hire DigitalCodeLabs