PHP Arrays

In JavaScript the elements in any array you create can only be accessed via its numerical position within the array. If you attempt to create an associative array in JavaScript (where the elements are named) then JavaScript creates an object instead of an array and the various array methods are not available. PHP doesn't have this limitation as it supports both numerical and associative arrays and in fact if you create an associative array you can still reference the values by their numerical position.

PHP also lacks the shortcut way of defining arrays by simply surrounding the array content in (square) brackets. PHP however doesn't require the 'new' keyword that the long version of the JavaScript declaration needs. The other difference is that PHP uses lowercase for the first letter of the word 'array'.

$numArray = array(10,20,30);
$numArray2 = array(1=>10,2=>20,3=>30);
$assocArray = array('first'=>'A','second'=>'B');

The first of these array declarations works the same way that an array declaration works in JavaScript with 10,20 and 30 being assigned to positions 0, 1, and 2 of the array respectively.

The second of these shows a shortcut way that PHP provides for when you want to leave out some of the positions in the array and assign values to specific positions within the array. In this case we have omitted to assign a value to position 0 in the array.

The third example shows how to define an associative array in PHP. This is a true associative array with the elements being able to be referenced either by the key names assigned or by their positions within the array.

PHP also differs from JavaScript in how you add extra elements to the end of an array. Where JavaScript requires you use the push() method or calculate the exact numerical position, PHP allows you to simply omit the value inside the (square) brackets and the next entry beyond what is already in the array is assumed.

Because PHP started out as a procedural language with object oriented processing being added later, what are methods of the Array object in JavaScript are simply separate functions in PHP. PHP provides a lot of different functions for manipulating arrays and has supported equivalents to many of the recently added JavaScript Array methods since long before these methods were added to JavaScript. In fact there are a number of array functions in PHP that still do not have built in equivalents in JavaScript and while some of these relate to the fact that PHP supports associative arrays while JavaScript doesn't there are also some such as shuffle() that can be recreated in JavaScript.

Perhaps one of the most useful ways to loop through an array in PHP is to use a foreach loop where you provide the name of the array to loop over and the variable name to use to reference the key and the value of each element as you process it. The recently added JavaScript forEach method uses a different syntax and can only reference the numerical positions rather than key names but is similar in its functioning to this PHP command.

foreach ($assocArray as $key => $value) {
// content here can refer to the key and value of each element of the array


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow