Solved

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

Posted on 2001-06-12
7
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find RGB colors from a screen. 2 41
how to use jquery variable in mysql query 20 77
How can I use javascript variable in mysql query 21 41
Position image fpdf 4 18
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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.

739 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