shamuraq Posted June 27, 2009 Share Posted June 27, 2009 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? Quote Link to comment Share on other sites More sharing options...
Alex Posted June 27, 2009 Share Posted June 27, 2009 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]; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.