kellz Posted November 6, 2007 Share Posted November 6, 2007 *looks weirdly at the subject* anyhoo.. so I've been reading about arrays (i already know the basics) and I want to do something totally kewl but im not sure how.. anyway here is the array type that im trying to figure out: $myClass = array( 'jason' => 'retard', 'shane' => 'cute', 'emma' => 'bestest friend ever!', 'sara' => 'back stabbing b*tch!', 'ricky => 'genius', 'sean' => 'lovely boyfriend', 'tim' => 'funny person', 'tanya' => 'eats to much', 'elin' => 'doesn't eat enough', 'martyn' => 'SEXYYYYYYYY', 'neil' => 'smokes....(not just cigarettes!)', 'Gary' => 'need's to wash more often (smelly)', 'Anna' => 'I don't know her so i assume she's evil', 'Sophie' => 'Paarrrtyyy!!! <3 <3', 'Mark' => 'stop fighting in school', 'Hannah' => 'bring it on b*tch!', 'Amy' => 'owed me £5 for over a year lol', 'Charlotte' => 'I am not a geek I am a nerd! and your ugly so shutup.' ); ok so thats most of my class (in real life ofcourse) and i want to know how i can choose a random person in that array and then print his/her name and the description of that person. I know how to get the description but i dont know how to get the names? (keys?) thanks Quote Link to comment Share on other sites More sharing options...
Mirkules Posted November 6, 2007 Share Posted November 6, 2007 I'm afraid you can't randomly select a number an use it to reference the array, if the array is associative. What you would have to do is pick a random number and then loop through the array that many times until you reach the desired key. For example: $random = rand(0, count($myClass)-1); //picks a number between 0 and the number of elements in your array $selected_key = ""; for ($i = 0; $i < $random; $i++) next($myClass); //the array pointer is pointing at the selected value $key = key($myClass); $value = current($myClass); Quote Link to comment Share on other sites More sharing options...
kellz Posted November 6, 2007 Author Share Posted November 6, 2007 thank you Quote Link to comment Share on other sites More sharing options...
revraz Posted November 6, 2007 Share Posted November 6, 2007 Sophie sounds fun. Quote Link to comment Share on other sites More sharing options...
kellz Posted November 6, 2007 Author Share Posted November 6, 2007 lol we are crazy together! a few months ago she got drunk for her first time and she tried to go on her scooter and fell off and chipped a tooth lol it was so funny she couldn't smile because she had half her front tooth missing, but it's fixed now. (until next time^^) and we often cause trouble in school.. but.. like.. innocent kinda trouble... does that even exist? lol anyhoo yesh she's fun. 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.