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... Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/ 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. Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/#findComment-619846 Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Author Share Posted August 19, 2008 Ok, thanks. Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/#findComment-619847 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. Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/#findComment-619848 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 Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/#findComment-619853 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. Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/#findComment-619857 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; Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/#findComment-619859 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. Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/#findComment-619863 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 Link to comment https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/#findComment-619865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.