Jump to content

Randomizing Array from Database


isimpledesign

Recommended Posts

Hi Guys

 

I was just wondering if their is a function to randomize an array pulled from database is it possible i know their is array_rand() but it doesnt seem to work for me can someone show me where i am going wrong or do i have to do it from the actual SELECT statemant i.e ORDER_RAND(); ?

 

Here is what my array prints out.

 


$user_list = get_users_by_specialism($specialism);

	print_r($user_list);

outputs.

Array ( [0] => 129 [1] => 46 [2] => 57 [3] => 109 [4] => 92 [5] => 137 [6] => 238 [7] => 101 [8] => 60 [9] => 90 [10] => 112 [11] => 133 [12] => 121 [13] => 220 [14] => 275 [15] => 278 ) 

 

I basically need to make this array output in a random order everytime not by ID incrementing.

 

Any help Please

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/218372-randomizing-array-from-database/
Share on other sites

You want to use 'order by rand()' for this. There's no point in returning every row within the database, loop through and store in an array, and then randomly sort that.. to do something you could do with a simple order by statement in the query.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.