Solved

array_rand() question

Posted on 2013-12-11
4
193 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 108

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

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

867 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

22 Experts available now in Live!

Get 1:1 Help Now