Articles in Basics Category

PHP Arrays

Arrays are variables which can hold more than one value at a time. Arrays are useful when you want to store a group of data. Consider an employee in a company called Robin Jackman with staff ID NX-97246 and a basic salary of 5000 dollars. You can represent this employee in a PHP script as below.

$empName = 'Robin Jackman';
$empId = 'NX-97246';
$empSalary = 5000;

This is fine for one employee. But what if you want to represent more employees? You would need to define three variables for each. Then remembering and accessing them may not be that easy. You can address this problem by putting these values into an array.

$emp1 = array('Robin Jackman', 'NX-97246', 5000);

You can access and print elements of this array as below.

echo $emp1[0]; // Robin Jackman
echo $emp1[1]; // NX-97246
echo $emp1[2]; // 5000

If you want to define another employee, you can follow same approach.

$emp2 = array('Taylor Edward', 'NX-97281', 4700);

Now think you want to have all the employees in one array. You can have it as below.

$employees = array($emp1, $emp2);

You can access elements of $employees array as below.

echo $employees[0][0]; // Robin Jackman
echo $employees[0][1]; // NX-97246
echo $employees[0][2]; // 5000
echo $employees[1][0]; // Taylor Edward
echo $employees[1][1]; // NX-97281
echo $employees[1][2]; // 4700

$employees is called a multi-dimensional array because it’s an array of arrays (Its elements are arrays).

Indexed vs Associative Arrays

Each array element has two parts as key and value. In $emp1[0], 0 (which is inside brackets) is the key and Robin Jackman is the value. Array keys let us locate elements. In $emp1 array, keys are 0, 1 and 2. When array keys are numeric, we call them Indexed arrays. You have to remember that Indexed arrays start from key 0 (not from 1).

You could also define $emp1 array as below and access its elements.

$emp1 = array('name'=>'Robin Jackman', 'id'=>'NX-97246', 'salary'=>5000);

echo $emp1['name']; // Robin Jackman
echo $emp1['id']; // NX-97246
echo $emp1['salary']; // 5000

When the keys of an array are strings we call it an Associative array. It’s easy to remember and call array elements in Associative arrays because it’s easy to remember names than remembering ordered numbers.

Think that it’s required to define another seven properties for employees. If it’s an Indexed array you have to remember order and position of keys to call a particular element. But in Associative arrays, you just need to remember the name of the key. For an example, you can easily remember and call $emp1['salary'] than $emp1[2].

Defining Arrays

You already saw a way to define arrays using array() function. You can also define an array by directly assigning a value to an element. Assume that $emp1 array hasn’t been defined yet.

$emp1[0] = 'Robin Jackman' // Makes $emp1 an array

For Associative arrays,

$emp1['name'] = 'Robin Jackman' // Makes $emp1 an array

For Indexed arrays, you can also initiate and add elements as below (using empty brackets).

$emp1[] = 'Robin Jackman';
$emp1[] = 'NX-97246';
$emp1[] = 5000;

In above case, numeric keys will automatically be assigned according to the order the elements were defined. You can access and print elements of this array just as you did previously.

echo $emp1[0]; // Robin Jackman
echo $emp1[1]; // NX-97246
echo $emp1[2]; // 5000

Array Functions

PHP provides a collection of built-in functions for working with arrays. Together with these functions, arrays have become a powerful feature of PHP.

echo count($emp1) // Will print 3, number of elements in the array

Assuming that $emp1 was defined as an Associative array,

$keys = array_keys($emp1); // $keys become an array that contains keys of $emp1

echo $keys[0]; // name
echo $keys[1]; // id
echo $keys[2]; // salary

For all available array functions, refer PHP manual.

Where to Head from Here...
Share with Your Peers...

We Value Your Feedback...

We love to hear what you think about this article. Please provide your opinion, suggestions and improvements using following form. Note that submitted feedback is not displayed but we will get back to you if it needs a reply.