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,....);

Example

<?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;

Example

<?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.

Example

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

This will produce following result

Red Science Sunday

To print a comma separated list of an array

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

Example

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

This will produce following result

Red,Science,Sunday

To reverse an array

This can be done using array_reverse() function.

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

To process more than one element of an array

This can be done using array_slice() function.

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

To trim an array

This can be done using array_splice() function.

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

To count number of elements in an array

This can be done using count() function.

Example

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

This will produce following result

6


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.