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 Link to comment https://forums.phpfreaks.com/topic/69892-solved-array_rand/ 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 Link to comment https://forums.phpfreaks.com/topic/69892-solved-array_rand/#findComment-351049 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. Link to comment https://forums.phpfreaks.com/topic/69892-solved-array_rand/#findComment-351054 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? Link to comment https://forums.phpfreaks.com/topic/69892-solved-array_rand/#findComment-351058 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 Link to comment https://forums.phpfreaks.com/topic/69892-solved-array_rand/#findComment-351061 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? Link to comment https://forums.phpfreaks.com/topic/69892-solved-array_rand/#findComment-351073 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 Link to comment https://forums.phpfreaks.com/topic/69892-solved-array_rand/#findComment-352291 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 Link to comment https://forums.phpfreaks.com/topic/69892-solved-array_rand/#findComment-352311 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.