Custom Search

Monday, February 25, 2013

PHP ARRAY


PHP ARRAY


ARRAY

  • An array is a data structure that stores one or more values in a single value.
  • For experienced programmers it is important to note that PHP's arrays are actually maps (each key is mapped to a value).
  • The array() function is used to create an array.

Syntax

Specifying with array()

An array can be created using the array() language construct. It takes any number of comma-separated key=> value pairs as arguments.
array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)

There are three types of arrays:

  1. Indexed arrays - Arrays with numeric index
  2. Associative arrays - Arrays with named keys
  3. Multidimensional arrays - Arrays containing one or more arrays



Indexed arrays -Numeric Array

  • These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero.


Example

Following is the example showing how to create and access numeric arrays.
Here we have used array() function to create array. 
This function is explained in function reference.
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
  echo "Value is $value
";
}
/* Second method to create array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";

foreach( $numbers as $value )
{
  echo "Value is $value
";
}
?>


This will produce following result:

Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is one
Value is two
Value is three
Value is four
Value is five

Associative Array

  • The index position is start from 0, the second is index position is 1 and so on. 
  • PHP supports associative array, in which we can associate any kind of key (generally string or numeric) with a value.

Use

Sometimes it is more beneficial to have String keys or indexes in an array instead of numeric keys. 
For instance if you want to store the salary of  employee or the marks obtained by the students, then it will be more beneficial to use associative array instead of using numeric array.

Initializing an Associative array

$array=array("City"=>"New Delhi", "State"=>"Delhi"," Pin"=>110029)

Like the numerically indexed array we can create and initialize an associative array with one element at a time. 

Following example will help you to understand this concept:

Example:

$array["Sedan"]="Manza";
$array['suv']="Safari";
$array['nano']=2009;
print_r($array);
?>
Output:

Array ( [Sedan] => Manza [suv] => Safari [nano] => 2009 )

Multi-dimensional Array

  • Multi-dimensional 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.
  • Multidimensional array is a structure which holds various arrays in an array.
  • Multi-dimensional array value are accessed using multiple index.

Example

In this example we create a two dimensional array to store marks of three students in three subjects:

Multi-dimensional Array example

   
    $marksheet = array(
         "Nitin" => array
          (
          "physics" => 82,        
          "maths" => 70,          
           "chemistry" => 89       
          ),
        "Sachin" => array
        (
         "physics" => 70,
         "maths" => 85,
         "chemistry" => 89
        ),
        "Priyanka" => array
        (
        "physics" => 81,
        "maths" => 72,
        "chemistry" => 90
        )
        );
   /* Multi-dimensional array values accessing */
   echo "Marks for Nitin in physics : " ;
   echo $marksheet['Nitin']['physics'] . "
";
   echo "Marks for Sachin in maths : ";
   echo $marksheet['Sachin']['maths'] . "
";
   echo "Marks for Priyanka in chemistry : " ;
   echo $marksheet['Priyanka']['chemistry'] . "
";
    ?>


Output




No comments:

Post a Comment