Php Arrays

An array is a special variable, which can store multiple values in one single variable.
There are three kind of arrays in php:
  • Numeric array - An array with a numeric index
  • Associative array - An array where each ID key is associated with a value
  • Multidimensional array -Array elements can contain one or more array.

Numeric Array

Numeric Array can be created by two ways.
1. Auto index.In this index will assigned automatically.
$bikes = array("Hero","Bajaj","Yamaha");
2. We can assign index manually also :
$bikes[0]="Hero";
$bikes[1]="Bajaj";
$bikes[2]="Yamaha";

Associative Arrays

In associative array there is a key value pair
With associative arrays we can use the values as keys and assign values to them.

Example :

In this example we use an array to assign ages to the different persons:

$personages = array("Sujal"=>32, "Mitesh"=>30, "Hiren"=>34);

$personages['Sujal'] = "32";
$personages['Mitesh'] = "30";
$personages['Hiren'] = "34";

Multidimensional Arrays

Multidimentional array means each element of array can also be a array.

Example :

$Arrayresult = Array
(
[Admin] => Array
  (
  [0] => Sujal
  [1] => Mitesh
  [2] => Hiren
  )
[Receptionist] => Array
  (
  [0] => Glenn
  )
[Staff] => Array
  (
  [0] => Rahul
  [1] => Sandhya
  [2] => Suraj
  )
)
We can access staff details by $Arrayresult['staff'][0]  or $Arrayresult['staff'][1] or  $Arrayresult['staff'][2]  

1 comment:

Kamila said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Asp.Net Training in Chennai
Software Testing Institutes in Chennai
Java Training Institutes in Chennai with 100 Placement
PHP Certification in Chennai