![]() |
|
#1
|
|||
|
|||
Using the For loop with an array and rand();I'm making a test for myself (and for others to use) on PHP. I made an array called $que. I'm going to add questions to the array, but for now i just put some words in each to test it out to see if it works with a for loop to generate random questions. Here is my code:
PHP Code:
It works, except that $x sometimes repeats the same number due to the fact that its limited to a very small range of numbers. Is there a way to ensure that $x does not repeat within the for loop? |
|
#2
|
||||
|
||||
Re: Using the For loop with an array and rand();Until someone else comes up with a better solution, maybe you can try something like this, using array_rand():
PHP Code:
Try it a couple of times, notice the values displayed are the keys to your original array and sorted randomly each time you refresh the page. If you decide to use this, just do a loop through this $ids and echo the lines. No chance for duplicates in this situation, for sure. |
|
#3
|
|||
|
|||
Re: Using the For loop with an array and rand();thanks, yeah it didnt repeat. but how can i use $ids in my loop? and also, whats the difference between using print_r and echo? i played with it and noticed that echo didnt properly display $ips like print_r did. thanks a lot for the help though, im new to php as you might notice.
Quote:
|
|
#4
|
||||
|
||||
Re: Using the For loop with an array and rand();I use print_r or var_dump, when I want to see what's in an array (or object). You should read the manual about these functions. I can't explain it better than what's already stated there.
http://www.php.net/manual/en/function.print-r.php http://www.php.net/manual/en/function.var-dump.php To complete your script, this could work: PHP Code:
|
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help in battleship | study007 | C Programming Language | 14 | 26-Nov-2006 09:28 |
| Quick, Insertion, and Partition | silicon | C++ Forum | 0 | 18-May-2005 20:49 |
| Merge and Heap...which is really faster | silicon | C++ Forum | 0 | 10-May-2005 13:46 |
| Insertion Sort Problem | silicon | C++ Forum | 2 | 08-May-2005 16:13 |
| Sorting Algorithms by Time | silicon | C++ Forum | 4 | 02-May-2005 21:54 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The