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
https://forums.phpfreaks.com/topic/163884-solved-array-random/
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
https://forums.phpfreaks.com/topic/163884-solved-array-random/#findComment-864682
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.