0% found this document useful (0 votes)
19 views

Chapter 4 Arrays in PHP

Uploaded by

abaasmuuse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Chapter 4 Arrays in PHP

Uploaded by

abaasmuuse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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;

Creating Arrays in PHP


In PHP, the array ( ) function is used to create an array.

There are two ways to create an arrays:

The index can be assigned automatically (index always starts at 0), like this:

$cars = array ("Volvo", "BMW", "Toyota");

or the index can be assigned manually:

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] . ".";
?>

Get the Length of an Array

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);
?>

Loop Through an Indexed Array

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] . ", ";

To add array elements:


$numbers = array (1, 26, 11, 13, 14, 17, 5, 7, 9, 21, 32, 2, 4, 41, 52);
$total = 0;
for ($i = 0; $i < count($numbers); $i++)
$total += $numbers[$i];
echo "<br>Total = $total";

To add even numbers of an array:


$numbers = array (1, 26, 11, 13, 14, 17, 5, 7, 9, 21, 32, 2, 4, 41, 52);
$total = 0;
for ($i = 0; $i < count($numbers); $i++)
if ($numbers[$i] % 2 == 0)
$total += $numbers[$i];
echo "<br>Total = $total";

Sorting Arrays

The elements in an array can be sorted in alphabetical or numerical order, descending or


ascending.

Use sort ( ) function to sort arrays in ascending order, for example:

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], ";

You can add two arrays and print, for example:


$array1 = array (1, 2, 3, 4, 5);
$array2 = array (6, 7, 8, 9, 10);
for ($i = 0; $i < 5; $i++)
$array3[$i] = $array1[$i] + $array2[$i];
for ($i = 0; $i < 5; $i++)
echo $array3[$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>";
}

Finally, study the following example and observe the output:


<?php
echo ("<center><b>Multi-dimensional arrays in PHP</b></center>");
$Arr = array
( array ( 2, 7, 6, 4),
array(5, 3, 9, 8 )
);

5
for ($i = 0; $i <= 1; $i++)
{
for ($j = 0; $j <= 3; $j++)
{
echo $Arr[$i][$j]." ";
}
echo ("<br>");
}
?>

You might also like