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
https://forums.phpfreaks.com/topic/120310-solved-arrays-curious/
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.

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.

Archived

This topic is now archived and is closed to further replies.

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