0

I create this in php for generate random numbers

> <?php
> 
> $products=array("1","2","3","4","5","6","7","8","9","10");
> 
> for ($i=0;$i<count($products);$i++) {
> 
> $numbers=rand(0,count(products));
> 
> print "".$products[$numbers]."<br>";
> 
> } ?>

I try generate in bucle different numbers but always show me the same numbers 1212121212 and nothing more , how i can generate this string or array and for example finally show 2 3 4 1 5 6 7 9 8 10 , and if reload script other combination

Thank´s Regards !!!

2
  • count(products) shouldn't it be count($products)? Commented Nov 7, 2012 at 21:26
  • 1
    You could simply shuffle the array. Commented Nov 7, 2012 at 21:27

2 Answers 2

2

You forgot the $ in count($products). As a result, the parser is treating it as the string "products", which has a count() of 1. Therefore, the rand() function returns zero or one, which in your original array correspond to "1" and "2".

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

1 Comment

Yes i solve that i put bad when i insert the post , but the problem continue , because many numbers repit all time and no able get different numbers and no repit , thank´s
1

Try using shuffle,

$products=array("1","2","3","4","5","6","7","8","9","10"); 
shuffle($products);
foreach($products as $v)
 echo $v; 

Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.