Jump to content

[SOLVED] Arrays - Curious


Andy-H

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.