PHP: Types of Arrays

Arrays are means of storing related data (a collection of variables) in programming languages. This article on Java arrays gives a good and simple introduction to arrays. Yeah! It’s a different programming language from PHP but the concept of arrays is the same across different programming languages (the code snippets below are of the PHP language).

Numeric Arrays

They use numbers as access keys i.e.

$variable_name = array(n=>value,...);

$fruits = array(0=>'Apple',

Associative Arrays

They use descriptive names for id keys i.e.

$variable_name = array('key_name'=>value);


Multidimensional Arrays

These are arrays that contain nested arrays. They allow programmers to group related data. A good usage of this in real world software is the creation of Drupal custom forms especially in pre-processor functions that are found within the template.php or .module files.

$movies = array(
'comedy'=>array('Pink Panther','Big Momma'),
'action'=>array('Die Hard','Expendables'),
'epic'=>array('The Lord of the Rings'),
'Romance'=>array('Romeo and Juliet')

August 1, 2014

Posted In: Information Technology

Tags: ,

Leave a Comment