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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.