isimpledesign Posted November 11, 2010 Share Posted November 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218372-randomizing-array-from-database/ Share on other sites More sharing options...
Adam Posted November 11, 2010 Share Posted November 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218372-randomizing-array-from-database/#findComment-1132963 Share on other sites More sharing options...
isimpledesign Posted November 11, 2010 Author Share Posted November 11, 2010 Ok thanks for the reply adam i guest im just trying to cut corners its on a buddypress website so ill have to trail through and find that function. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/218372-randomizing-array-from-database/#findComment-1132967 Share on other sites More sharing options...
Adam Posted November 11, 2010 Share Posted November 11, 2010 I provided a link to the MySQL manual for it in my post. Although it's incredibly easy to use: select * from table order by rand() limit 10; Quote Link to comment https://forums.phpfreaks.com/topic/218372-randomizing-array-from-database/#findComment-1132971 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.