Extract Data the Smart Way with array_column()

PHOTO EMBED

Thu Aug 14 2025 19:02:58 GMT+0000 (Coordinated Universal Time)

Saved by @agungnb #php

$users = [
    ['id' => 1, 'name' => 'Alice'],
    ['id' => 2, 'name' => 'Bob'],
    ['id' => 3, 'name' => 'Charlie'],
];

To get all the names:
$names = array_column($users, 'name');
// Result: ['Alice', 'Bob', 'Charlie']

Want to index them by ID?
$names = array_column($users, 'name', 'id');
// Result: [1 => 'Alice', 2 => 'Bob', 3 => 'Charlie']
content_copyCOPY

array

https://medium.com/@catcatduatiga/php-arrays-like-a-pro-7-powerful-tricks-youll-wish-you-knew-sooner-92e33e836ed1