Jump to content

[SOLVED] Array Random


shamuraq

Recommended Posts

Hi guys,

I've a problem with random that i really need help with...

 

Scenario:

$coin = array('0.01'=>'1 cent','0.05'=>'5 cents','0.1'=>'10 cents','0.2'=>'20 cents','0.5'=>'50 cents','1.0'=>'$1');

I now need to randomize the array where i need to store them into into 2 seperate variables to be called out later. Eg if the randomised array is '0.01' and '1 cent',

i need to store it as:

$a = '0.01';

$b = '1 cent';

 

I checked out PHP manual.net and saw this:

$b=array( 'G'=>'Good', 'B'=>'BAD', 'O'=>'OK' );
echo array_rand($b); // 'G' or 'B' or 'O'
echo $b[array_rand($b)]; // 'Good' or 'BAD' or 'OK'

The problem with that is:

if i define $a = array_rand($coin);

i cannot define $b since $b would have to be: $b[array_rand($coin)]

and then it would re randomise the array again...

 

How do i obtain $a and $b from just one array_rand?

Link to comment
Share on other sites

Is the key supposed to always correspond to the value? If so couldn't you just store the key only then get the value using that key? If you insist in storing it within 2 values (kinda defeats the purpose of an array). You can do this:

 

$arr = array( 'G'=>'Good', 'B'=>'BAD', 'O'=>'OK' );
$a = array_rand($arr); 
$b = $arr[$a];

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.