Jump to content

[SOLVED] array stuff


kellz

Recommended Posts

*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

             

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

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.