ki Posted September 19, 2007 Share Posted September 19, 2007 okay basically my problem is that my info isnt displaying. my code: $aFriends = array($p1->friends); $rFriends = array_rand($aFriends, 6); for ($i=0; $i<=6; $i++) { echo $aFriends[$rFriends[$i]]; } $p1->friends is pulled from a mysql db and has for example: 1, 3, 4, 5, 7, 9, 10, 15 what I wanted it to do was pull 6 random numbers from the array and its not doing that :/ any help would be appreciated Quote Link to comment Share on other sites More sharing options...
hemlata Posted September 19, 2007 Share Posted September 19, 2007 Hello , Modified your code with the following, $aFriends = array($p1->friends); $rFriends = array_rand($aFriends, 6); for ($i=0; $i<6; $i++) { echo $rFriends[$i]; } Hope this will solve your problem. Regards Quote Link to comment Share on other sites More sharing options...
ki Posted September 19, 2007 Author Share Posted September 19, 2007 thanks for the reply, but unfortunatly it doesnt work, i have no clue why. Quote Link to comment Share on other sites More sharing options...
hemlata Posted September 19, 2007 Share Posted September 19, 2007 ok. what is the array format you are having for which random results needs to be found.. Are you getting any error messages? Quote Link to comment Share on other sites More sharing options...
ki Posted September 19, 2007 Author Share Posted September 19, 2007 okay my array would end like this: $aFriends = array(1,2,3,4,5,6,7); $rFriends = array_rand($aFriends, 6); for ($i=0; $i<6; $i++) { echo $rFriends[$i]; } I want it to randomly choose the numbers that are found in the array. so for example it would ed out like: 5,1,7,3,2,4 and so on and no im not having any error issues Quote Link to comment Share on other sites More sharing options...
ki Posted September 19, 2007 Author Share Posted September 19, 2007 would it actually be possible to add each random selection from the $aFriends array and make a new array entirely and implode it? Quote Link to comment Share on other sites More sharing options...
ki Posted September 21, 2007 Author Share Posted September 21, 2007 can someone help me with this? im still getting nothing from different alterations to my code, I have no idea why its messing up like this Quote Link to comment Share on other sites More sharing options...
ki Posted September 21, 2007 Author Share Posted September 21, 2007 okay with a bit of tampering i figured out how to do it: srand((double)microtime()*1000000); $aFriends = explode(",", $p1->friends); foreach ($aFriends as $key => $term) { $aFriends[$key] = $term; } $rFriends = array_rand($aFriends, 2); echo $aFriends[$rFriends[0]]; echo $aFriends[$rFriends[1]]; hope that helps someone in the future Quote Link to comment 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.