PHP Arrays

by Cyle
Categories: php
Tags: ,
Comments: 1 Comment

In php, arrays contain data (also known as values) per key. The key can be an integer or a string, and the value can be any php data type. Let’s start with the basics…

Creating an Array

To create a new array we use the array() function:

$myarray = array();  //creates an empty array

We use the same function to create an array with values:

$myarray = array('blue','green','red','yellow');  //creates an array with values

To print or use the values in an array, we have to use the key to point to which value we want:

$myarray = array('blue','green','red','yellow');  //creates an array with values
echo $myarray[0];  //blue
echo $myarray[2];  //red

Notice how ‘0’ is the first key. When a key is not expressed when creating an array it defaults to integers starting at ‘0’. We can change the key to a string or any integer we would like. We assign values to keys using the => operator:

$myarray = array('first' => 'blue'
	, 'second' => 'green'
	, 13 => 'red'
	, 99 => 'yellow');

If we wanted to add a value to the above array and did not express a key, php will use the largest integer plus one. In the above example the next key if not expressed would be 100.

Adding/Retrieving Values

If you use the array() function to add values to an existing array, the contents on the existing array will be deleted and the new array will start from a clean slate. When adding values to an array, keys are optional. You can add values to arrays with or without using keys.

$myarray['first'] = 'blue';  //saves 'blue' to the array at key 'first'
$myarray[43] = 'green';  //saves 'green' to the array at key 43
$i = 10;
$myarray[$i] = 'red';	//saves 'red' to the array at key 10 since $i=10
$myarray[] = 'yellow';	//saves 'yellow' to the array at key 44 (largest integer key + 1)

Using the example above, let’s now retrieve a value from the array. Here, keys are necessary:

echo $myarray[43];	//green
$value = $myarray[44];	//$value = yellow
echo $my array['first']; //blue

It is always good practice to use quotes when the key is a string.

Deleting Values From Arrays

To delete a value or entry in an array we use the unset() function:

unset($myarray[43]);	//removes green from the array at key 43
unset($myarray);		//deletes the entire array contents

Print Array Contents

Here is a simple way to display the contents of an array:

foreach($myarray as $key => $value) {
  echo $key . ' => ' . $value . '<br />';

Multi-Dimensional Arrays

Arrays can contain arrays as values, making them multi-dimensional arrays. We still have to use the array() function to initialize it:

$my2darray = array(
  'first' => array(34 => 'green', 10 => 'blue', 'third' => 'red'),
  '2' => array('one' => 'yellow', 2 => 'pink'),
  '13' => array('white','black','grey'),
  '4' => array()
$myvalue = $multiDimensionalArray['first'][10]; //$myvalue='blue'
echo $multiDimensionalArray['13'][1]; //black
echo $multiDimensionalArray['2']['one']; //yellow

Reindexing Arrays

If you assign values to an array and then delete a value from the middle of the array, the array does not reindex or change its keys. To remove gaps in keys when you delete them we use the array_values() function:

$myarray1 = array(0 => 'white', 1 => 'blue', 2 => 'green', 3 => 'red', 4 => 'yellow');
/*deleted green and produces an array defined as:
$myarray1 = array(0 => 'white', 1 => 'blue', 3 => 'red', 4 => 'yellow');
without the value at key 2 and NOT
$myarry1 = array(0 => 'white', 1 => 'blue', 2 => 'red', 3 => 'yellow');
$myarray2 = array_values($myarray1);
// Now $myarray2 is defined as array(0 => 'white', 1 => 'blue', 2 => 'red', 3 => 'yellow')

For a list of common functions and examples see Common PHP Array Functions.

1 Comment »

  1. […] a value to a key in an array we use the “=>”. More information on that can be found here. We can also combine other operators with the assignment operators. Examples below:<?php $a = 5; […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Today is Thursday