Solved

array_rand() question

Posted on 2013-12-11
4
191 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 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
That's right.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MYSQL simple update statement 3 32
.htaccess file settings 4 32
PHP Sum Column in Table 3 26
php connect() failed error 25 12
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 …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

744 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