[programming] PHP – Array

Array is a variable that saves some of data. When a normal ‘$a’ variable  (scalar variable) able to save a single value at a time, array variable capable of saving some values simultaneously in one time. Array can have many elements, each elements hold a single value. In PHP you’ll find that index in array is not only in number, but also in string (associative array).

as we learn that scalar variable ($a) saves a single value (PHP-intro), so if multiple values is inserted, $a variable will hold the last inserted values and delete all the previous values.

$a = 14;  //$a saves 14

$a = 70; //$a saves 70, the previous values (14) is deleted.

as for array variable, it allows you to collect/save more than one values at a time. it is a collection of scalar variable. the values it self is saved into each elements of array.

for example there are several number that need to be saved,

14 70 90

just like the table displayed above, that’s exactly how array works. It saves the value into each indexed cell, the default cell will be indexed as ‘0’.

There are several ways to make and insert values into an array variable :


$color = array (“Red”,”Yellow”,”Green”,”Blue”);

this array allow you to insert values simultaneously. When you used this way to insert values into an array variable, the index will be start on ‘0’ in the left value, and will be increased by one for next index each.


$color[] = “Red”;
$color[] = “Yellow”;
$color[] = “Green”;
$color[] = “Blue”;

Just like the previous one, the index here also start from ‘0’ with the value of ‘Red’ as the first data executed by the program.


$color[0] = “Red”;
$color[1] = “Yellow”;
$color[3] = “Blue”;
$color[2] = “Green”;

this time you define the index the values should goes to. so, it doesn’t matter if you switch the line as long as you know where index should be used.

Just like the various way of insert the value into the numerical indexed array, it also has various way of displaying the values of the array, here is the option you can use :

  1. Foreach
    foreach($color as $value)
    echo “the color we have are $value”;
  2. Standard Looping
    for($i = 0; $i <= 3; $i++)
    echo “The color we have are $color[$i]”;

I like the last one, it can be modified to the extend of complicated.

Associative array

Associative array use string to index the value instead of number.

example :

$data = array (“Ani” => 75, “Budi” => 80, “Andi” => 75, “Wati” => 80);


$data[“Ani”] = 75;
$data[“Budi”] = 80;
$data[“Andi”] = 75;
$data[“Wati”] = 80;

To print the value of associative array we’ll use foreach

foreach($data as $name => $value)
echo “$name got $value”;

Multidimensional array
Array as explained above is type of one dimensional array, means that those array only has one row. Multidimensional array has more than that. it has row(s) and column(s).

Ani 90 75 88
Budi 95 77 80
Wati 85 70 75

Just like table shown above, 2 dimensional array have both rows and columns. and index is start from the value in top-left cell. Default index number is 0. To insert or call the value we need to declare the rows and column. $variable_name[number_of_rows][number_of_column].

so Ani is located in index [0][0] //row 0 and coloumn 0
Budi is located in index [1][0] //row 1 and column 0
’88’ is located in index [0][3] //row 0 and column 3
and so on.

to insert the value into 2 dimensional array you may use one of the following method :

$test[0][0] = “Ani”;
$test[0][1] = 90;
$test[0][2] = 75;
$test[0][3] = 88;

$test[1][0] = “Budi”;
$test[1][1] = 95;

and so on until you get to

$test[2][3] = 75;


Second Method,

$test = array (array(“Ani”,90,75,88), array(“Budi”,95,77,80), array(“Wati”,85,70,75);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s