Solved

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

Posted on 2001-06-12
7
220 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Not needed 13 57
Web Reply Form - PHP with Upload 4 20
Post xml via PHP Soap Client 3 6
Datepicker in PHP 9 14
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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

760 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

16 Experts available now in Live!

Get 1:1 Help Now