Skip to content
PHP Logo

How can the relationship between usernames and levels be maintained in PHP arrays?

In order to maintain the relationship between usernames and levels in PHP arrays, one solution is to use associative arrays where the usernames act as keys and the levels as values. This way, you can easily retrieve the level of a specific username by referencing its key in the array.

php
$userLevels = [
    'john_doe' => 5,
    'jane_smith' => 3,
    'alice_jones' => 7
];

// To retrieve the level of a specific username
$username = 'jane_smith';
$level = $userLevels[$username];
echo "The level of $username is $level";