How can user groups be implemented in PHP for managing different access levels with shared passwords?

To implement user groups in PHP for managing different access levels with shared passwords, you can create an associative array where the keys represent the user groups and the values are arrays of usernames and passwords for each group. When a user logs in, you can check their credentials against the appropriate group in the array to determine their access level.

<?php

// Define user groups with usernames and passwords
$userGroups = [
    'admin' => [
        'admin' => 'adminpass',
        'superadmin' => 'superpass'
    ],
    'user' => [
        'john' => 'userpass',
        'jane' => 'userpass'
    ]
];

// Simulate user login
$username = 'admin';
$password = 'adminpass';
$loggedIn = false;

// Check user credentials against user groups
foreach ($userGroups as $group => $users) {
    if (array_key_exists($username, $users) && $users[$username] === $password) {
        $loggedIn = true;
        echo "User $username logged in as $group.";
        break;
    }
}

if (!$loggedIn) {
    echo "Invalid username or password.";
}

?>