Протестировать можно тут — PHP Sandbox
function hash_sort($array, $hash_string){
$output = array();
$used_keys = array();
$circle = 0;
sort($array);
$hash = str_split(preg_replace("/[^0-9]/", '', sha1($hash_string)));
for($i = 0; $i <= 9; $i++){
if(!in_array($i, $hash)){
$hash[] = $i;
}
}
$temp_hash = $hash;
while(count($output) < count($array)){
if(count($temp_hash) == 0){
$temp_hash = $hash;
$circle++;
}
$key = intval($circle.array_pop($temp_hash));
if(!in_array($key, $used_keys) AND !empty($array[$key])){
$output[] = $array[$key];
$used_keys[] = $key;
}
}
return $output;
}
// Тест
$test_array = array();
for($i = 0; $i < 5; $i++){
$test_array[] = "item ".$i;
}
//Случайный порядок
print_r(hash_sort($test_array, "qwerty"));
//Случайный порядок
print_r(hash_sort($test_array, "12345"));
//Случайный порядок, но такой же, как и с предедущи ключем
print_r(hash_sort($test_array, "12345"));
Комментарии (0)
Не писать ответ