Solved

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

Posted on 2001-06-12
7
221 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now