$users = [ ['id' => 101, 'name' => 'Alice', 'email' => 'alice@example.com'], ['id' => 102, 'name' => 'Bob', 'email' => 'bob@example.com'], ['id' => 103, 'name' => 'Charlie', 'email' => 'charlie@example.com'], ['id' => 104, 'name' => 'David', 'email' => 'david@example.com'], ]; // Method 1: Using a foreach loop (explicit and clear) $usersById = []; foreach ($users as $user) { $usersById[$user['id']] = $user; } // Method 2: Using array_reduce (more functional) // $usersById = array_reduce($users, function ($carry, $user) { // $carry[$user['id']] = $user; // return $carry; // }, []); echo "Original users array: "; print_r($users); echo " Users mapped by 'id': "; print_r($usersById); // Example of accessing an element by its ID directly $alice = $usersById[101]; echo " Accessed user 'Alice' by ID: "; print_r($alice);