PhpMyanmar.com logo Php Tutorial
Home
Php Tutorial
 Echo
 If

Arrays
Show Myanmar Version
 show-myanmar-font
Printer Friendly Page

An array is a data structure that stores one or more values in a single value.

Studying an example should make us understand array more.

PHP - A Numerically Indexed Array

PHP Code:
$student_array[0] = "Bobby";
$student_array[1] = "Sam";
$student_array[2] = "Charlie";
$student_array[3] = "Clive";

In the above example we use a single variable "$student_array".

We made use of the key / value structure of an array. 

The keys were the numbers we specified in the array and the values were the names of the peoples. 

Each key of an array represents a value that we can manipulate and reference. 

The general form for setting the key of an array equal to a value is:

$array[key] = value;

If we wanted to reference the values that we stored into our array, the following PHP code would get the job done.

PHP Code:
echo "Two of my employees are "
         . $employee_array[0] . " & " . $employee_array[1];
echo "
Two more employees of mine are "
        . $employee_array[2] . " & " . $employee_array[3];
Display:
Two of my employees are Bob & Sally
Two more employees of mine are Charlie & Clare

There is another way of specifing an array, we called it an associative array.

PHP - Associative Arrays

Instead of using number as the key of an array, some descriptive name can be used as a key.

See the example below:-

PHP Code:
$salaries["Bob"] = 2000;
$salaries["Sally"] = 4000;
$salaries["Charlie"] = 600;
$salaries["Clare"] = 0;

echo "Bob is being paid - $" . $salaries["Bob"] . "
";
echo "Sally is being paid - $" . $salaries["Sally"] . "
";
echo "Charlie is being paid - $" . $salaries["Charlie"] . "
";
echo "Clare is being paid - $" . $salaries["Clare"];
Display:
Bob is being paid - $2000
Sally is being paid - $4000
Charlie is being paid - $600
Clare is being paid - $0

Usefulness of arrays will become clearer when we gets to for, loops and while loops statements.

Note For more detail please see Php Manual at http://www.php.net

Processing time -- 0.0123 seconds.

f6ca0f7f542931908c82119e849b4e1a