-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;
4
  • 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. Commented May 5, 2023 at 23:26
  • Hi, I tried to shuffle but with no luck, it still outputs duplicate numbers like 2;100|2;100 Commented May 5, 2023 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. Commented May 6, 2023 at 0: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). Commented May 6, 2023 at 0:53

2 Answers 2

-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

Sign up to request clarification or add additional context in comments.

1 Comment

Than you! this code worked for me, added delimiters separately.
-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;

2 Comments

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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.