-1

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;
  • For a relatively large amount of elements from a smaller set it is probably best to shuffle the list and then take the number of required elements from the head of the list. If the set is too large to fit in memory you can try and take elements and reject if it was already in a set of taken elements. – Maarten Bodewes May 05 '23 at 23:26
  • Hi, I tried to shuffle but with no luck, it still outputs duplicate numbers like 2;100|2;100 – Фероз Хамиди May 05 '23 at 23:43
  • If you want to add pairs of items to the array, the add an array, eg: `$kats[] = [$kat, 100];` Re-parsing strings like is fragile and takes unnecessary time. I can see that you're already having to dance around picking your list delimiters to avoid problems. – Sammitch May 06 '23 at 00:35
  • @ФерозХамиди This is a known technique. I don't know how you can get 2 and then 2 again. You need to shuffle the *original* `array(1,2,3,4)`, right? Then you cannot get 2 and 2 again (unless it is another array that contains duplicate values, of course). – Maarten Bodewes May 06 '23 at 00:53

2 Answers2

-1

You can use array_unique to remove duplicates before you implode the array

$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';

}

$kats = array_unique($kats);

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

$line=str_replace('--', ';', $line);
$line=str_replace('-', '|', $line);
 
 echo $line;
Googster
  • 9
  • 2
  • 1
    Thank you, this works well, but only one problem: the output shd be at leasr two numbers, like this 3;100|2;100, and when i add array_unique it sometimes outputs only on number, as i understand when rhere is duplicate output the array_unique prevents from echoing seconnd or third numbers. – Фероз Хамиди May 06 '23 at 00:29
  • While this can be fixed by getting a sub-array & introducing a loop in case there are too few elements, I think this would overly complicate the solution - especially since there are better solutions readily available. – Maarten Bodewes May 06 '23 at 00:52
-1
$kat_list=array(1,2,3,4);
shuffle($kat_list); // shuffle list
$count=3;
for($i=0; $i<$count; ++$i) {
    // get the first N items of the shuffled list
    printf("%d ", $kat_list[$i]);
}

Try it: https://3v4l.org/a31pv

Sammitch
  • 30,782
  • 7
  • 50
  • 77