Jump to content

[SOLVED] array_rand


ki

Recommended Posts

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

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

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

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.