Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

PHP Development Part 4 – Arrays

Wednesday, February 15th, 2012 by Servage

This part of the course is about Arrays. Arrays are a datatype in PHP that you can use to store data. You can look at an array like a chain. It can contain multiple elements, each element can contain multiple sub-elements. Imagine, the chain can divide in every link, becoming a new chain, with endless possibiliteis of where it can lead. Let’s start with a simple example:

$my_array = array();
$my_array[] = 'Element 0';
$my_array[] = 'Element 1';

$my_other_array('Element 0', 'Element 1');

The code above creates an array called $my_array, whereafter it appends to values to the array. You can also do the same thing in one-line, by adding the elemenets like done in the last line. The type of array above is an array with numeric keys. That means, each element in the array has a numeric index key, needed when using the value of the array elements. Example:

$element0 = $array[0];
$element1 = $array[1];

You see that you retrieve the array-elements by adding the key to the square brackets. Beware that the array-element-counting starts at zero (instead of one). The alternative to numeric keys is named keys, which basically means you have a unique word as keys instead. Example:

$my_array = array();
$my_array['elem0'] = 'Element 0';
$my_array['elem1'] = 'Element 1';

$my_array = array('elem0'=>'Element 0', 'elem1'=>'Element 1');

You see now that you need to define the individual keys for this type of array. Retrieval is done similarly to numeric keys:

$element1 = $array['elem1'];
$element2 = $array['elem2'];

Please note that you can store arrays within arrays, creating so-called nested arrays. Example:

$my_array = array();
$my_array[] = array();
$my_array[][] = 'Element 0 in Subarray 0';
$my_array[][] = 'Element 1 in Subarray 0';

You could retrieve these elements like this:

$elements = $my_array[0]; // Giving you the whole subarray
$element1 = $my_array[0][0]; // Giving you element 1 in the subarray

The same thing is possible with named keys like this:

$my_array = array();
$my_array['subarray'] = array();
$my_array['subarray']['elem0'] = 'Element 0 in Subarray 0';
$my_array['subarray']['elem1'] = 'Element 1 in Subarray 0';

// Alterantive way of defining the array
$my_array = array(
	'subarray' => array(
		'elem0' => 'Element 0',
		'elem1' => 'Element 1',
	)
);

// Retrieve with
$elem1 = $my_array['subarray']['elem1'];

I hope this gave you a little insight into the world of arrays – one of the most useful ways of handling larger amounts of data in an organized way.

Stay tuned, and look forward to future PHP courses, tying some of the gatehred knowledge together.

PHP Development Part 4 - Arrays, 2.8 out of 5 based on 4 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.