ZCE Arrays: Associative Arrays
Associative Arrays are the same as Enumerative arrays except in the fact that they are not idexed by numbers (but can be) instead they are indexed by a name. If you read Enumerative arrays you will not that Ringo was indexed as 3, in an Associative array he would be indexed as “drummer”. Like such
$Beatles['drummber'] = "Ringo"; echo $Beatles['drummber']; //Will output Ringo
Associative Arrays Facts:
- index can be numbers or letters (words)
- very simular to Enumerative
- output same as enumerative including foreach loop.
Creating a Associative Array:
Their are many ways to create an associative array, and it almost the same as Enumerative arrays except that you need a name therefore require “speechmarks” and such.
$Beatles = array(); $Beatles['singer1'] = 'Paul'; $Beatles['singer2'] = 'John'; $Beatles['guitarist'] = 'George'; $Beatles['drummer'] = 'Ringo'; // --- OR --- OR ---- OR ----- $Beatles['singer1'] = 'Paul'; $Beatles['singer2'] = 'John'; $Beatles['guitarist'] = 'George'; $Beatles['drummer'] = 'Ringo'; // --- OR --- OR ---- OR ----- $Beatles = array('singer1' => 'John', 'singer2' => 'Paul', 'guitarist' => 'George', 'drummer' => 'Ringo');
As you can see we use wrap ‘ around the element name, otherwise it may cause an error. Like with Enumerative arrays you can initiate an array using array() or if your lazy you don’t have to. You can also initiate an array using a list as shown in the 3rd example of the simple methods shown above it.
Thanks To:
I have high knowledge of php arrays, but I would like to point out these resorces for good examples if what im talking about only makes sense to me:
http://www.learnphp-tutorial.com/Arrays.php
http://www.informit.com/articles/article.aspx?p=31840&seqNum=3
No Comments »
RSS feed for comments on this post. TrackBack URL











