Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to use foreach() to ensure unique array values?

Posted on 2001-06-12
7
Medium Priority
?
244 Views
Last Modified: 2008-03-06
Hi,
Has anyone used foreach() to ensure the values in an array are unique ?  I need to ensure I have 10 unique values, generated randomly (and below 75) and I cant get foreach to work. Any suggestions or examples greatly appreciated.

Cheers

Russell
0
Comment
Question by:russellc
  • 5
  • 2
7 Comments
 
LVL 2

Expert Comment

by:higijj
ID: 6182635

foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br>\n";
}

where $key is the index of your array, and $value is its value. $arr is the array..
0
 

Author Comment

by:russellc
ID: 6183935
Thanks higijj, but the problem is to ensure that each element within the array is unique. I randomly select 10 numbers below 100, using mt_rand() and assign them to an array; this regularly produces an array with duplicate elements. The problem is how to remove the duplicates and replace them with unique values.
0
 
LVL 2

Expert Comment

by:higijj
ID: 6184040
then why don't you store each random number into

$arr[1]
$arr[2]
$arr[3]
...
$arr[10]

and use the array function: array_rand();

srand ((double) microtime() * 10000000);
$arr[1]
$arr[2]
$arr[3]
...
$arr[10]
$rand_keys = array_rand ($arr, 1);
print $input[$rand_keys[0]]."\n";

that would give you one randomly choosen value of $arr

I know this wouldn't ensure no 2 index are the same.. but this might be a way to get around that!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:higijj
ID: 6184053
you might wanna look at the array function: shuffle
0
 
LVL 2

Expert Comment

by:higijj
ID: 6184054
Oh! Just got your point.. you need 10 differents number, correct?!
0
 
LVL 2

Accepted Solution

by:
higijj earned 150 total points
ID: 6184059
if so, you might want to use: array_unique
http://www.php.net/manual/en/function.array-unique.php

so basically, you would set your array with 10 numbers..
apply array_unique, then use count($array) to check if it still has 10 elements, if so, you're ready to go, if not, generate others elements to have a total of 10 and re-apply array_unique ..

repeate the above until you've got 10 differents elements!


Hope this help,
Higijj!
0
 

Author Comment

by:russellc
ID: 6189406
You have put me onto the right track Higijj,

I now have the array_unique() and count($array) working to create a array of 10 unique numbers. Thanks for the advice.

$number = 0;
     while ($number<10)
    {
     for ($K=1; $k<11; $k++)
     {
       $rnd_array[$k] = mt_rand( 1, 65);
       $rnd_array = array_unique ($rnd_array);
       $number = count ($rnd_array);
     
     }
     }
         




Russell
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question