Jump to content

basic array syntax 'as' and =>


anatak

Recommended Posts

Can someone point me to a good tutorial that explains the basic syntax of arrays ?

AS
I know how to use foreach($array AS $var){ } but I don't really know what the syntax is supposed to mean.

 

=>
what does the => mean in ($array AS $var => $value){}?

 

I keep running into basic problems with arrays and I am trying to understand how to manipulate arrays.

 

the problem is that I can get things more or less working without really understanding what I am doing.

 

thank you very much.

Link to comment
Share on other sites

I know how to use foreach($array AS $var){ } but I don't really know what the syntax is supposed to mean.

'as' generally indicates that you are aliasing something. In the context of foreach it indicates that the items in the array will be assigned to the given variables within the loop body. Basically what that structure says in english is:

For each item in $array, assign the item's value to $var and execute the instructions below.

 

 

 

what does the => mean in ($array AS $var => $value){}?

=> indicates a key and value pair. On the left side of => is the key, and on the right is the value.

 

So what that line is saying in english would be:

For each item in $array, assign the item's key (aka index) to $var and the item's value to $value and execute the instructions below.

 

=> may also be used when creating an array, such as

$array = array(
   'key' => 'value'
   //, 'more' => 'pairs...'
);
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.