Friday 11 December 2015

PHP Array

PHP Array
All the variables you have used have held only a single value.
Array variables are “special” because they can hold more than one value in one single variable.
There are three types of array:-
Indexed array : An array with a numeric index
Associative array : An array where each key is associated with a value

Multidimensional array : An array containing one or more array

How to define an array
 FIRST METHOD:-
<?php
  $arr = array(10,11,12,13,14,15);
  echo $arr[0];
?>

2nd method
<?php
$arr[ ]=10;
$arr[ ]=20;
$arr[ ]=30;
$arr[ ]=40;
$arr[ ]=50;
  echo $arr[0];
?>

3rd method
<?php
$arr[0]=10;
$arr[1]=20;
$arr[2]=30;               
$arr[3]=40;               
$arr[4]=50;      
  echo $arr[0];
?>

PHP Associative Array

In associative array index( key ) can initialized according to Your own requirement.
An array in PHP is actually an ordered map. A map is a type that associates values to keys.
In this association use ( => ) sign to define index and values.

There are two ways to create an associative array
First Way to define associative Array
<?php
$Personage=array("Ravi"=>"30","Vishu"=>"21","Harmeet"=>"43");
echo "Ravi is ".$Personage["Ravi"]."Years old";
?>
Another Way to define associative Array
<?php
$Personage['Ravi']=30;
$Personage['Vishu']=21;
$Personage['Harmeet']=43;
echo "Harmeet is ".$Personage["Ravi"]."Years old";
?>

Loop Through an Associative Array
<?php
 $state=array("Dl"=>"Delhi","Hr"=>"Haryana","Pn"=>"Punjab","Br"=>"Bihar");   
foreach($state as $val)
       {
  echo $val." ";
       }
?>
Output
Delhi Haryana Punjab Bihar

Using foreach display index(key) value of $state array
<?php
$state=array("Dl"=>"Delhi","Hr"=>"Haryana","Pn"=>"Punjab","Br"=>"Bihar");
    
foreach($state as $key=>$val)
{
  echo $key."---".$val."<br/>";
}
?>
Output 
Dl — Delhi
Hr — Haryana
Pn — Punjab
Br — Bihar

PHP Indexed Array
        PHP numeric array
Numeric array can stores numbers, strings etc.
Default array index will be represented by numbers.
By default array index starts from 0
In PHP array( ) is used to crate array.Inside this can pass multiple values separated by comma( , )
Syntax:- 
  array(value1,value2..)

Example
<?php
  $arr=array(10,20,30,40,50);
  $col=array("red","green","blue","black");
  //print first value of $arr and $col array
  echo $arr[0];
  echo $col[0];
?>

We can define array in three ways
<?php 
$col=array("blue","red","green","white","pink");

//OR
$col[ ]="blue";
$col[ ]="red";
$col[ ]="green";

//OR
$col[0]="blue";
$col[1]="red";
$col[2]="green";
?>

Loop Through an Numeric(Indexed) Array
Find the sum of given array
<?php      
$sum=0;
$arr=array(10,20,30,40,50);
for($i=0;$i<count($arr);$i++)
{
  $sum=$sum+$arr[$i];
}        
echo "Sum of given array = ".$sum; 
?>

Display all the colors name i.e stored in array
<?php      
$col=array("blue","red","green","white","pink");
for($i=0;$i<count($col);$i++)
{
  echo $col[$i]." ";
}
?>
Find Sum of even and odd numbers stored in array
<?php
$arr=array(10,11,12,13,14,15);
for($i=0;$i<count($arr);$i++)
{  
  if($arr[$i]%2==0) 
  {   $even=$even+$arr[$i];         }
  else 
  {  $odd=$odd+$arr[$i];           }
}
echo "Sum of even=".$even."<br/>";
echo "Sum of odd=".$odd;
?>

PHP Nested Array
Nested / Multi-dimensional Array
A multidimensional array is an array containing one or more arrays.
The dimension of an array indicates the number of indices you need to select an element.
For a two-dimensional array, you need two indices to select an element
For a three-dimensional array, you need three indices to select an element.
Syntax
array(
  array(val1, val1, val2..),
  array(val1,val2,val3..)
       );
Create a two dimensional numeric array and find the sum.
<?php
 error_reporting(‘ERROR’);
$arr=array(array(10,10,10),array(10,10,10),array(10,10,10));
$s=0;  $s1=0;
//using for loop:
for($i=0;$i<3;$i++)
{  for($j=0;$j<3;$j++)
  {  echo $arr[$i][$j]." ";
  $s=$s+$arr[$i][$j];
  }
  echo "<br>";
}
echo "sum of array:".$s;
?>


Create a two dimensional numeric array and find the sum using for-each
<?php
 error_reporting(‘ERROR’);
$arr=array(array(10,10,10),array(10,10,10),array(10,10,10));
$s=0;  $s1=0;
//using foreach loop:
foreach($arr as $k)
  foreach($k as $v)
     {
     echo $v;
     $s1=$s1+$v;
     }
echo "<br>";
}
echo "sum of array:".$s;
?>

More Examples
Two dimensional associative array( user’s name and mobile number)

Two dimensional associative array

Multidimensional associative array
Syntax
array(
  array(
  array(
  array(val1, val1, val2....)
  )
  )
  );