RE: [PHP] Unique Array

From: Date: Mon, 10 Sep 2001 16:08:00 +0000
Subject: RE: [PHP] Unique Array
Groups: php.general 
Request: Send a blank email to php-general+get-66770@lists.php.net to get a copy of this message
> -----Original Message----- > From: Deirdre Mc Cann [mailto:mccanndee@hotmail.com] > Sent: Monday, September 10, 2001 6:30 AM > To: php-general@lists.php.net > Subject: [PHP] Unique Array > > > Can anyone help me? I need to produce a random and uniques > array using numbers between 0 and 9. I tried to create a > random array and them make it unique. > > for ($i = 0; $i<100; $i++){ > $a[$i] = rand(0,9); > $test=$a[$i]; > } > > $ra = array_unique($test); > > I keep getting an error saying $test is the incorrect type. Because you've set $test equal to one element of $a (making it an integer). Array_unique() wants to operate on an entire array. What I'd do, assuming I'm understanding the question correctly, would be something more like: $a = range(0,9); srand((double)microtime()*1000000); shuffle($a); (That is, get all your values loaded first, then just randomly rearrange the order.) --- Mark Roedel | "Blessed is he who has learned to laugh Systems Programmer | at himself, for he shall never cease LeTourneau University | to be entertained." Longview, Texas, USA | -- John Powell

« previous php.general (#66770) next »