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
Solved

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

Posted on 2001-06-12
7
228 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress French and English Site 6 77
Number of hours between date in DB and now 8 21
How is this connection happening? 3 20
What does this formatting equate to? 7 18
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

829 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