Solved

array_rand() question

Posted on 2013-12-11
4
196 Views
Last Modified: 2013-12-12
I want to randomly choose items in my array and keep their values.

when I run this on my array
print_r($array_local_ids);

echo "</br>";

print_r(array_rand($array_local_ids,2));

Open in new window


it prints this
Array ( [0] => 175 [1] => 174 [2] => 177 [3] => 179 [4] => 176 )
Array ( [0] => 2 [1] => 4 )

i want the values of the first array in my random array not the keys of the first array.
0
Comment
Question by:Luey
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39712685
The keys is what it returns.  http://us.php.net/manual/en/function.array-rand.php  It does not change the array.  The idea is that you use the keys in a 'foreach' to 'randomly' access the original array.
0
 

Author Comment

by:Luey
ID: 39712730
ok yep done that before.  I want them back in an array so I can use them in sql so I quess I have to create that new array in foreach.  Is that right?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39712803
That's right.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39713902
If you want all of the values in random order, you can copy the array and use shuffle().  If you want to choose one element at random, you can use end() after shuffle().
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 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