PHP Arrays-Types and how they are created?

Arrays make an integral component of any programming language. It is true for PHP as well. PHP arrays can be used to store multiple values in same variable using different indices or keys. Arrays are important for string multiple data elements in program.

Ways of creating PHP arrays-

In PHP, arrays can be created in three different ways-

  • Using the array keyword
  • Using the element keys to create associative array
  • Initialising individual elements of array using index of element and assigning values.

Using the array Keyword

This is the simplest way to create PHP arrays is using array keyword.  The elements of the array must be of same data type. In this technique the array variable is declared using the following syntax.

$ArrayName= array(list of elements);

Example:

$items=array(‘Pen’,’Notebook’,’Suitcase’,’Telephone’);

The elements of an array declared by this method can be accessed or modified by using the index of the element with array name.

echo $items[1];

displays the second element of the array stored at index 1

$items[3]=’Smartphone’;

Replaces ‘Telephone’ in items array with ‘Smartphone’

$items[4]=’Cabinet’;

A new element ‘Cabinet’ will be added at the end of the array items.

Using the array Keyword and keys to create an Associative Array

Associative array is a list of multiple elements of same data type identified by a key value for each element. It is defined with array keyword but each element is preceded by a unique key. The syntax is

$ArrayName= array(key1=> value1, key2=>value2,…..);

Here key can be a string and value can be of any data type that you want to make an array of.

Example:

$items=array(‘p’=>‘Pen’, ‘N’=>’Notebook’, ‘S’=>’Suitcase’, ‘T’=>’Telephone’);

The elements of an array declared by this method can be accessed or modified by using the index of the element.

echo $items[‘N’];

displays the second element of the array stored with key ‘N’

$items[‘T’]=’Smartphone’;

Replaces ‘Telephone’ in items array with Smartphone

$items[‘C’]=’Cabinet’;

A new element ‘Cabinet’ will be added at the end of the array items.

Initialising individual elements of Array using Index or key of Element

In this way of creating PHP array you don’t need to use array keyword. The array is creating by using the array name and index in square brackets and assigning value to the array element. It is similar to assigning a value to a variable. The syntax is

$arrayname[index]= value;

Or

$arrayname[key]= value;

Example:

$MyFavoriteMovies[0]=’Edge of Tomorrow’;

$MyFavoriteMovies[1]=’Intersellar’;

$MyFavoriteMovies[2]=’Gravity’;

$MyFavoriteMovies[3]=’Independence Day’;

In this method if an index is missed in series while assigning value, that element is considered NULL and will not be the part of array.

The elements of array created by this method can be accessed in similar way that of an array created with array keyword.

Example

 <?php
echo "<h4> Using array keyword</h4>";
$items=array(10,20,40,60,80);
echo $items[3].",". $items[1]."<br>";
$items[3]=100;
echo $items[3].",". $items[1]."<br>";
$items[5]=120;
echo $items[3].",". $items[5]."<br>";

echo "<h4> Using keys </h4>";
$items=array('p'=>'Pen', 'N'=>'Notebook', 'S'=>'Suitcase', 'T'=>'Telephone');
echo $items['N'].",". $items['T']."<br>";
$items['T']='Smartphone';
echo $items['N'].",". $items['T']."<br>";
$items['X']='Xmas Tree';
echo $items['X'].",". $items['T']."<br>";

echo "<h4> By Assigining values to elements with index </h4>";
$MyFavoriteMovies[0]='Edge of Tomorrow';
$MyFavoriteMovies[1]='Intersellar';
$MyFavoriteMovies[2]='Gravity';
$MyFavoriteMovies[3]='Independence Day';
echo $MyFavoriteMovies[3].",". $MyFavoriteMovies[1]."<br>";

echo "<h4> By Assigining values to elements with key </h4>";
$MyFavoriteMovies['E']='Edge of Tomorrow';
$MyFavoriteMovies['I']='Intersellar';
$MyFavoriteMovies['G']='Gravity';
$MyFavoriteMovies['D']='Independence Day';
echo $MyFavoriteMovies['E'].",". $MyFavoriteMovies['G']."<br>";
?>

Output

PHP arrays example