Chapter 4 Arrays in PHP
Chapter 4 Arrays in PHP
Arrays are a set of variables which all have the same name, but each has a different index. An
array stores multiple values in one single variable and you can access the values by referring to
an index number. Each member of the array is called an element. You can create arrays in the
same way you create variables, as long as you remember to put the square brackets around them
to denote the index.
$SomaliCities[1] = "Muqdisho";
$SomaliCities[2] = "Hargeysa";
You do not have to assign them in order numerically, you can jump as few, or as many entries as
you want:
$SomaliCities[49] = "Kismayo";
$SomaliCities[13] = "Bosaso";
One last feature of arrays in PHP is that you can actually assign different data types and variables
to the values within the array. Here are some examples:
$Number[1]=24;
$Number[2]="twenty three";
$Number[2]=$variable;
The index can be assigned automatically (index always starts at 0), like this:
1
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
The count ( ) function is used to return the length (the number of elements) of an array:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
To loop through and print all the values of an indexed array, you could use a for loop, like this:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
for($x = 0; $x < count($cars); $x++) {
echo $cars[$x];
echo "<br>";
}
?>
2
You can create array of any types and loop trough it for manipulations. For example to print
elements:
$numbers = array (1, 26, 11, 13, 14, 17, 5, 7, 9, 21, 32, 2, 4, 41, 52);
for ($i = 0; $i < count($numbers); $i++)
echo $numbers[$i] . ", ";
Sorting Arrays
sort($numbers);
for ($i = 0; $i < count($numbers); $i++)
echo "$numbers[$i], ";
3
Use rsort ( ) function to sort arrays in descending order, for example:
rsort($numbers);
for ($i = 0; $i < count($numbers); $i++)
echo "$numbers[$i], ";
Multi-dimensional Arrays
A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of
arrays). They are useful when representing data that needs two sets of indexes, like coordinates
on a map or graph. PHP understands multidimensional arrays that are two, three, four, five, or
more levels deep. However, arrays more than three levels deep are hard to manage for most
people. You can carry on nesting arrays until PHP runs out of memory, which probably will be
in under 100 nested arrays. Multidimensional arrays are set up in the same way as normal arrays.
Note that the dimension of an array indicates the number of indices you need to select an
element.
First, take a look at the following table:
Name Stock Sold
Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15
4
We can store the data from the table above in a two-dimensional array, like this:
$cars = array
(
array ("Volvo", 22, 18),
array ("BMW", 15, 13),
array ("Saab", 5, 2),
array ("Land Rover", 17, 15)
);
Now the two-dimensional $cars array contains four arrays, and it has two indices: row and
column.
To get access to the elements of the $cars array we must point to the two indices (row and
column).
Example
for ($i = 0; $i < 4; $i++)
{
for ($j = 0; $j < 3; $j++)
echo $cars[$i][$j] . ", ";
if ($j == 3)
echo "<br>";
}
5
for ($i = 0; $i <= 1; $i++)
{
for ($j = 0; $j <= 3; $j++)
{
echo $Arr[$i][$j]." ";
}
echo ("<br>");
}
?>