Wednesday, March 23, 2011

Associative Arrays

An associative array,each ID key is associated with a value.
When storing data about specific named values,a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assigns values them.
Example 1:
In this example we use an array to assign ages to the different persons:
$ages=array("Peter=>32, "Quamire"=>30, "Joe"=>34);
Example 2:
This example is the sames as example 1,but shows a different way of creating the aray:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
The ID Keys can be used in a script:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is" . $ages['Peter'] . "years old.";
?>
The code above will output:
Peter is 32 years old.

2 comments:

  1. i tried it but giving an error

    by Ideal Programmer

    ReplyDelete
  2. Acesoftech is No.1 PHP training center in kolkata. Our professional trainer can provide you PHP training in such a way that it will be complete job

    oriented. So, if you are looking for professional PHP

    training in Kolkata
    , acesoftech
    is the best choice for you.

    ReplyDelete