Hi,
I have had experience with some programmers and they really needed a 101 with arrays. While many languages treat array mystically and allow you to freely access and modify array pointers, higher languages like PHP give you a limited access to them. Using pointers is kept simple but still there is enough room for all the play. People usually get confused when dimensions increase, but there is the same old trick of imagining a multidimensional array as if it were a matrix. Once you start to visualize arrays, they become as simple as algebra. But same as algebra they can do many things useful in regular programming.
Here is what php.net has to say about arrays in PHP:
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
So points to remember are for one they are "ordered maps". That means when you define arrays there order is saved. for example:
This will define a $brandnew array that will have values sparkling and shining. That means although the key a stores shining it will be second because it was listed second. One thing to note here is that this is not true for indexed arrays. Consider this:
Here if we are defining indexes the array will be sorted according to that. So this way even if we define shining second, it will occupy place before sparkling. There you have a difference in indexed and associative arrays which you can use for benefits. So when we run the following code:
The output will be:
On the other hand, running this code:
The output will be:
So as we see there is a difference in the output corresponding to the difference in the way we define arrays. That is all for today, more on arrays tomorrow i.e. multi-dimensional beasts.
Thanks a lot keep reading and do comment
--
Amit Kriplani
PHP Web/Application Developer
Blog:http://blog.amitkriplani.com
Mobile: +91-9913932644MSN: amitk87@hotmail.com
Gmail: amitkriplani5@gmail.com
Yahoo: amitkriplani@ymail.com
Skype: amitkriplani
Twitter: @amitk87
I have had experience with some programmers and they really needed a 101 with arrays. While many languages treat array mystically and allow you to freely access and modify array pointers, higher languages like PHP give you a limited access to them. Using pointers is kept simple but still there is enough room for all the play. People usually get confused when dimensions increase, but there is the same old trick of imagining a multidimensional array as if it were a matrix. Once you start to visualize arrays, they become as simple as algebra. But same as algebra they can do many things useful in regular programming.
Here is what php.net has to say about arrays in PHP:
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
So points to remember are for one they are "ordered maps". That means when you define arrays there order is saved. for example:
$brandnew = array('b'=>'sparkling', 'a'=>'shining');
This will define a $brandnew array that will have values sparkling and shining. That means although the key a stores shining it will be second because it was listed second. One thing to note here is that this is not true for indexed arrays. Consider this:
$brandnew = array(2=>'sparkling', 1=>'shining');
Here if we are defining indexes the array will be sorted according to that. So this way even if we define shining second, it will occupy place before sparkling. There you have a difference in indexed and associative arrays which you can use for benefits. So when we run the following code:
$brandnew = array('b'=>'sparkling', 'a'=>'shining'); foreach ($brandnew as $value) { print $value; }
The output will be:
sparklingshining
On the other hand, running this code:
$brandnew = array(2=>'sparkling', 1=>'shining'); foreach ($brandnew as $value) { print $value; }
The output will be:
shiningsparkling
So as we see there is a difference in the output corresponding to the difference in the way we define arrays. That is all for today, more on arrays tomorrow i.e. multi-dimensional beasts.
Thanks a lot keep reading and do comment
--
Amit Kriplani
PHP Web/Application Developer
Blog:http://blog.amitkriplani.com
Mobile: +91-9913932644MSN: amitk87@hotmail.com
Gmail: amitkriplani5@gmail.com
Yahoo: amitkriplani@ymail.com
Skype: amitkriplani
Twitter: @amitk87