Arrays are awesome. They are one of the easiest and most basic collection objects that you can use.

You probably know the basic form of an array. integer-index based:


array[0] = 'hello arrays!';

However you can also use more descriptive identifiers.
Say for example you want to create an array to represent a product, yes yes, you can probably create a class and assign the member variables accordingly, OR you can use the magic of associative arrays like so:

$product['name'] = 'Xbox 360';
$product['price'] = 20; // yeah right!!
$product['description']= 'Sweet gaming console';

so at any point you can access the info you need by referencing the “associated” index


print "Product price :" . $product['price'];

Another cool feature of associative arrays is the traversing options.

To loop through the array:


foreach($product as $key=>$value)
    print "$key : $value";

That’s pretty much it friends.

Associative arrays are extremely useful when parsing JSON data too, which we will cover in your next tutorial.

-WAREBOT

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>