Arrays

Updated on 28 Dec 2018

Defining an array

Array’s can be defined empty (no values), or with values defined.

$empty = [];
$busy_period = ['May', 'June', 'July'];
 
//display the contents of the array.
print_r($busy_period);

Adding a single element

There maybe circumstances where you need to ‘add’ elements to an existing array. The example below shows how to add elements to an array.

$busy_period = ['May', 'June', 'July'];
$busy_period[] =  'February';
 
//display the contents of the array.
print_r($busy_period);

Adding a single element with a key and value

The example below shows how to add a key indexed element to an array after its been defined.

$busy_period = [5 => 'May', 6 => 'June', 7 => 'July'];
$busy_period[2] = ‘February’;
 
//display the contents of the array.
print_r($busy_period);

When you add elements onto an array in the manner shown above, they are placed onto the end of the array, and this is reflected in the output. This may not be the desired effect because you may want the elements sorted based on their key value. This is covered below in the ksort and asort sections.

Adding multiple elements

You can add multiple elements to an array at the same time with the array_push function. This function adds the elements onto the end of the array list.

$busy_period = ['May', 'June', 'July'];
array_push($busy_period, 'January', 'February');
 
//display the contents of the array.
print_r($busy_period);

A simple for loop

This technique can be used only for arrays that are zero based, i.e. where you haven’t specified a value for the key. If you have specified keys for your array, then you’ll want to use a foreach loop.

$busy_period = ['May', 'June', 'July'];
 
//display the individual elements of the array
for($i = 0; $i < sizeof($busy_period); $i++)
  {
  echo "\n" . $busy_period[$i];
  }

A simple foreach loop

This technique uses the special foreach loop which cycles through each element of the array. This is the most common technique for cycling through elements of an array because the foreach loop works with any kind of index values.

$busy_period = [5 => 'May', 6 => 'June', 7 => 'July'];
 
//display the individual elements of the array
echo "\nUsing a foreach loop";
foreach($busy_period as $key => $value)
  {
  echo "\nKey Value: $key element value: $value";
  }

key, current and next

This technique shows how to use a standard for loop on an array that is not zero indexed based. It uses the key, current and next functions to retrieve the key, the value and move the pointer to the next element in the array.

$busy_period = [5 => 'May', 6 => 'June', 7 => 'July'];
 
//display the individual elements of the array
echo "\nUsing the current, key and next functions";
for($i = 0; $i < sizeof($busy_period); $i++)
  {
  $month_number = key($busy_period);        //get the key value (e.g. 5)
  $month_name   = current($busy_period);    //get the element value (e.g. ‘May’)
 
  echo "\nKey Value: $month_number element value: $month_name";
  next($busy_period);			    //move to the next array element.
  }

Sorting

The output of my array (from previous examples), is shown below. How do I sort the array so that it displays in the correct sequence?

ksort Array function

The ksort function sorts the array elements in alphabetical/numerical order based on their keys (hence the k in ksort).

$busy_period = [5 => 'May', 'June', 'July'];
$busy_period[2] = 'February';		//add an element onto the end of the array
 
ksort($busy_period);	//sort the array on the key index
print_r($busy_period);	//display the contents of the array

asort Array function

The asort function sorts the array elements in alphabetical/numerical order based on their element values.

$busy_period = [5 => 'May', 'June', 'July'];
$busy_period[2] = 'February';		//add an element onto the end of the array
 
asort($busy_period);			//sort the array on the element value
print_r($busy_period);		        //display the contents of the array