Andy-H Posted August 19, 2008 Share Posted August 19, 2008 I was just wondering what is the functionality of => in an array, for example: array( "dog" => "cat" => "bird" => "bug"); As the only arrays I have ever used are like: array("dog","cat","bird","bug"); As the topic suggests this question has no purpose to help me in a project I just made the topic because curiosity got the better of me. :-\ Thanks for any answers/explanations... Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 19, 2008 Share Posted August 19, 2008 It's used like this: $array = array("key" => "value", "key2" => "value2"); It's called an Associative Array. To call an value2 in $array you would do it like so: echo $array['key2']; You can also use it in foreach loops: foreach($array as $key => $value){ echo "Key is {$key} and value is {$value}.<br />"; } The other array that you have used is just a numerical array or Indexed Array. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Author Share Posted August 19, 2008 Ok, thanks. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 19, 2008 Share Posted August 19, 2008 No worries, if you need some more explanations check out this link. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Author Share Posted August 19, 2008 I read it thanks but your explanation was much better, straight to the point. Thanks for the help. Cant think of a use for it mind you, I'm sure I will find one some day but I'm only coding text based mafia MMORPG's at the moment. lol Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 19, 2008 Share Posted August 19, 2008 It can be useful. Small example that probably isn't the best but anyway. Say you want to store the users data in a session variable. You can only use one and store it like so: $_SESSION['userdata'] = array("username" => $username, "password" => $password, "email" => $email); echo $_SESSION['userdata']['email']; //echo out the users email There are many many more good uses for it as well. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Author Share Posted August 19, 2008 Nice, can you not do that just using a normal array tho??? Like: $_SESSION['userdata'] = array($username, $password, $email); $email = $_SESSION['userdata'].[3]; echo $email; Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 19, 2008 Share Posted August 19, 2008 Course you can, but sometimes it's nice to know you are actually calling 'email'. By making it associative your basically naming the elements in your array, like naming variables. People find it easier, and it does have its uses. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Author Share Posted August 19, 2008 Yeh I suppose it would be a little easier to remember when your calling it again 2 month down the line. lol Thanks 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.