How can one efficiently remove the ID from an array in PHP and only use it as a key?

When removing the ID from an array in PHP and using it only as a key, you can create a new array where the ID becomes the key and the values remain the same. This can be achieved by iterating through the original array and assigning the ID as the key in the new array. This way, you can efficiently access values using the ID as the key.

// Original array with IDs
$originalArray = [
    ['id' => 1, 'name' => 'Alice'],
    ['id' => 2, 'name' => 'Bob'],
    ['id' => 3, 'name' => 'Charlie']
];

// Create a new array with IDs as keys
$newArray = [];
foreach ($originalArray as $item) {
    $newArray[$item['id']] = $item['name'];
}

// Accessing values using the ID as the key
echo $newArray[2]; // Output: Bob