Considering an array of colors like this:
$cores = array('#000', '#333', '#666', '#999'); // do tamanho que você precisar
I would create a second array, associative, where you save the colors for each user. It starts empty:
$coresPorUsuario = array();
And the idea is that it will contain the colors of the users as they emerge. For example, array('VE' => '#000', 'ED' => '#333')
.
To control everything, use a function. It receives the initials of the user and returns the corresponding color. If the user already exists in $coresPorUsuario
, get the color that is there. If it does not exist, it takes the next available color, associates it with the user, and returns that color. You will also need a variable to control the next available color.
$proxima = 0;
function corUsuario($usuario) {
global $cores;
global $coresPorUsuario;
global $proxima;
// Usuário ainda não existe na array
if(empty($coresPorUsuario[$usuario])) {
// Guarda a cor do usuário e avança para a próxima cor disponível
$coresPorUsuario[$usuario] = $cores[$proxima++];
// Se passou da quantidade de cores disponíveis, começa novamente da primeira
$proxima = $proxima == count($cores) ? 0 : $proxima;
}
// Retorna a cor do usuário
return $coresPorUsuario[$usuario];
}
WARNING: The above example uses global variables because it is a short path as an example. You probably want to implement this function as a method of a class. In this case, use instance properties instead of global variables.
Testing:
echo corUsuario('AA') . "\n"; // #000
echo corUsuario('AB') . "\n"; // #333
echo corUsuario('AC') . "\n"; // #666
echo corUsuario('AA') . "\n"; // #000
echo corUsuario('AD') . "\n"; // #999
echo corUsuario('AE') . "\n"; // #000
Demonstration