Searching...
Tuesday 18 September 2012

Php Array of Arrays:Php Tutorial

11:02 pm

Php Array of Arrays:Php Tutorial

Php Array of Arrays:Php Tutorial:

An array is a concept of declaring a variable in which multiple values can be stored or an array stores multiple values in a single variable.
An array in php is of compound type .
An array is actually an ordered map. A map is a type that maps values to keys.
Each element in the array has its own index. An array can be created by the array (or) construct.
It takes a certain number of comma separated     key => value pairs.

Syntax :

Array(key=>value , key=>value,-----)
Count(array_name)   :   is a function of php used for getting the size of an array , the size of an array is the number of elements present in the array.
Print_r(array_name)  :  is a function used for displaying the entire contents of array with key.

In php there are three types of php array of arrays:

1.       Numeric arrays :
                              A numeric array stores each array element with a numeric index. There are two techniques to create a numeric array.
a.       Index is assigned automatically.
$arr = array(10,20,30,40,50);
b.      Assign the index manually.
$arr = array();
$arr[0] = 10;
$arr[1]  = 20;
$arr[25] = 30;
$arr[3]  = “india”;
c.       Foreach loop:
                                 This gives an easy way to iterate over arrays . foreach works only on arrays and objects.
There are two syntaxes  :
Foreach(array as $value)
{
  Statements
}
For every loop iteration the value of the current array element is assigned to $value and the array pointer is moved to next element so on the next loop iteration , it will be looking at the next array value.
Foreach( array as $key =>$value)
{
 Statements ;
}
This loop also does the same thing except that the current elements key/index will be assigned to the variable $key on each loop iteration.
Ex:
<?php
Echo “<h1>”;
$arr = array (10,20,30,”india”,123.45);
Echo “size of array =”.count($arr).”<br>”;
Print_r($arr);
Echo “<br>”;
$data = array();
$data[0] =”india”;
$data[11] = “limited”;
Echo “size of data array=”.count($data).”<br>”;
Foreach($arr as $val)
{
 Echo $val.” ,”;
}
Echo “<br><br>”;
Foreach($arr as $key =>$val)
{
 Echo $key.”=>”.$val,”,”;
}
?>
2.       Assosiative arrays :
                                            An array where each ID key is associated with a value; with assosiative arrays we can use the values as keys and assign values to them.
$ages = array(“sachin”=>39,”sehwag” => 33, “gambir”=>23,”dhoni”=>32);
Or
Ages = array();
$ages[‘sachin’]=39;
$ages[‘sehwag’]=33;
$ages[‘gambir’]=29;
$ages[‘dhoni’]=32;
If you donot specify a key for a given value , then the maximum number of the integer indices is taken till there , and the new key will be that maximum value +1.
If you specify a key that already has a value assigned to it , that value will be overwritten.
Eg:   
    $a = array(5=>43 , 32,56,”b”=>12);
Is same as   $a=array(5=>43 , 6=>32,7=>56,”b”=>12);
Eg:
<?php
 Echo “<h1>”;
$ages = array(“sachin”=>39,”sehwag”=>33,”gambir”=>29,”shareef”,10=>”india”,123,11=>”tech”);
Echo “size of array=”.count($ages).”<br>”;
Print_r($ages);
?>
3.       Multidimentional arrays :
                                              An array containing one or more arrays. In a multidimentional array ,each element in the main array can also be an array and each element in the sub array can be an array and so on.
If you want to remove a key/value pair from an array . you need to unset() it.
      Unset($arr[0]);
      Unset($arr);
Example :
<?php
Echo “<h1>”;
$data = array(“nums”=> array(10,20,30,40,50),”alphs”=>array(“A”,”B”,”C”,”D”),”inst”=>array(“peers”,”satyam”,”wipro”));
Echo “size of array =”.count(data).”<br>”;
Echo “size of nums =”.count($data[‘nums’]).”<br>”;
Print_r($data);
Echo “<br><br>”;
Foreach($data as $key =>$val)
{
  Echo “<u> contents of $key are </u><br>”;
Foreach($key as $v)
Echo $v.”,”;
Echo “<br>”;
Unset($data[‘nums’]);
Echo “size of array=”.count($data).”<br>”;
Unset($data);
Echo “size of array=”.count($data).”<br>”;
?>

0 comments:

Post a Comment