PHP Array

Whenever we want to store related data like list of books, list of students etc at one place, we use an array. An array not only allows to store related data but to organise it as you like and also quickly access to that data. Array can be effectively manipulated. In PHP, arrays are indexed from 0 and not from 1.

PHP has three types of arrays

  • Indexed or Numeric Array
  • Associative Array
  • Multidimentional Array

Array can be specified in following two different ways

  • With the use of array() construct
  • With the use of array operator[]

Specifying an array using array() construct

Syntax

$arrayName=array(var1,var2,....);

PHP Array Example 1

<?php 
$myChice=array("Red","Science","Sunday");
echo "My favorite colour is".$myChoice[0].", 
my fav subject is".$myChioce[1]."and my fav day is".$myChoice[2];
?>

This will produce following result

My favorite colour is Red, my fav subject is Science and my fav day is Sunday

Specifying an array using array operator[]

Syntax:

$arrayName['key']=value;

PHP Array Example 2

<?php 
$myChice[0]="Red";
$myChoice[1]="Science";
$myChoice[2]="Sunday";
echo "My favorite colour is".$myChoice[0].", 
my fav subject is".$myChioce[1]."and my fav day is".$myChoice[2];
?> 

This will produce following result

My favorite colour is Red, my fav subject is Science and my fav day is Sunday

To print a list of an array

This can be done using implode() function.

PHP Print a List of an Array Example

<?php 
$myChice=array("Red","Science","Sunday");
echo implode(" ",$myChoice);
?> 

This will produce following result

Red Science Sunday

Print a Comma Separated List of an Array

This can also be done using implode() function but differntly.

Print a Comma Separated List Example

<?php 
$myChice=array("Red","Science","Sunday");
echo substr(implode(',',$myChoice),0,-2);
?> 

This will produce following result

Red,Science,Sunday

Reverse an Array

This can be done using array_reverse() function.

Reverse an Array Example

<?php 
$myChice=array("Red","Science","Sunday");
echo "Original array:".substr(implode(',',$myChoice),0,-2);
echo "</br>";
$myChoice=array_reverse($myChoice);
echo "Reversed array:".substr(implode(',',$myChoice),0,-2);
?> 

This will produce following result

Original array: Red,Science,Sunday
Reversed array:Sunday,Science,Red

Process More than one Element of an Array

This can be done using array_slice() function.

Process Array Elements Example

<?php 
$myChice=array("Red","Science","Sunday","Brown","Arts","Tuesday");
$colChoice=array_slice($myChoice,0,3);
echo "My Colour Choice array:".substr(implode(',',$colChoice),0,-2);
?> 

This will produce following result

My Colour Choice array: Red,Brown

Trim an Array

This can be done using array_splice() function.

Trim an Array Example

<?php 
$myChice=array("Red","Science","Sunday");
echo "Original array:".substr(implode(',',$myChoice),0,-2);
echo "</br>";
$trmyChoice=array_splice($myChoice,2);
echo "Trimmed array:".substr(implode(',',$trmyChoice),0,-2);
?> 

This will produce following result

Original array: Red,Science,Sunday
Trimmed array:Red,Science

Count number of Elements in an Array

This can be done using count() function.

Count Array Elements Example

<?php 
$myChice=array("Red","Science","Sunday","Brown","Arts","Tuesday");
echo count($myChoice);
?> 

This will produce following result

6