vendredi 5 mai 2023

php random numbers without duplicate

I have php code below that creates random numbers when adding static number ;100 and divides numbers with |. It works well but creates duplicate numbers, how to make it prevent creating duplicate numbers?

echo shd be 3;100|2;100 but without duplicate numbers before ;100. but this code outputs numbers like 2;100|2;100 sometimes, that are duplicates.

<?php
$kat_list=array(1,2,3,4);

$count_cat= rand(2,3);

for ($i = 1; $i <= $count_cat; $i++) {
    $rnd_cat= rand(0,count($kat_list)-1);
    $kats[]=$kat_list[$rnd_cat].'--100';

}
$line=implode('-',$kats);

 $line=str_replace('--', ';', $line);
 $line=str_replace('-', '|', $line);
 


 echo $line;



Aucun commentaire:

Enregistrer un commentaire