How can the desired array structure be achieved more efficiently in PHP?

To achieve the desired array structure more efficiently in PHP, we can use the array_reduce function to iterate over the original array and build the desired structure in a single pass.

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

$desiredArray = array_reduce($originalArray, function ($result, $item) {
    $result[$item['id']] = $item['name'];
    return $result;
}, []);

print_r($desiredArray);