You are on page 1of 21

Chapter 4

Arrays in PHP
Introduction
• Arrays are a set of variables which all have the same name,
but each has a different index (a number or string).
• 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.
• Some arrays are referenced by numeric indices; others allow
alphanumeric identifiers.

2
Introduction (cont…)
• Built-in functions let you sort them, add or remove sections,
and walk through them to handle each item through a
special kind of loop.
• And by placing one or more arrays inside another, you can
create arrays of two, three, or any number of dimensions.

3
Introduction (cont…)
• One last feature of arrays in PHP is that you can actually
assign different data types and variables to the values within
the array.
$Number[1] = 24;
$variable = 5.7;
$Number[2] = "twenty three";
$Number[0] = $variable;

4
Numerically Indexed Arrays
<?php
$paper[] = "Copier";
$paper[] = "Inkjet";
$paper[] = "Laser";
$paper[] = "Photo";
echo $paper[0]; //prints Copier
echo $paper[2]; //prints Laser
?>
• You can use print_r function to display information about an array in
human-readable form. The _r stands for in human readable format.
5
Example
• For example: print_r($paper);

• You can also use var_dump function to displays structured


information about variables including its type and value.
• For example: var_dump ($paper);

6
Using Arrays Function
In PHP, the array ( ) function is used to create an array.
For example:
$paper = array ("Copier", "Inkjet", "Laser", "Photo");
• To loop through and print all the values of an indexed array,
you could use a for loop, like this:
for ($i = 0; $i < count ($paper); $i++)
echo ("$paper[$i], ");

7
Using Arrays Function
• You can create array of any types and loop trough it for
manipulations. For example to print elements:
$numbers = array (26, 11, 13, -4, 14, 17, 5 , 52, 7, 9, 21, 32, 2,
4, 5);
for ($i = 0; $i < count($numbers); $i++)
echo ("$numbers[$i], ");

8
The foreach...as Loop
• You can use foreach statement to step through all the items
in an array, one at a time, and do something with them.
• The process starts with the first item and ends with the last
one, so you don’t even have to know how many items there
are in an array.
• For example:
$numbers = array (26, 11, 13, -4, 14, 17, 5 , 52, 7, 9, 21, 32, 2,
4, 5);
foreach ($numbers as $n)
echo ("$n, ");
9
Adding array elements
$numbers = array (26, 11, 13, -4, 14, 17, 5 , 52, 7, 9, 21, 32, 2,
4, 5);
$total = 0;
foreach ($numbers as $n)
$total += $n;
echo ("<br>Total of all elements is: $total");

10
Adding two arrays
$array1 = array (1, 2, 3, 4, 5);
$array2 = array (6, 7, 8, 9, 10);
//Adding the two arrays
for ($i = 0; $i < count($array1); $i++)
$array3[$i] = $array1[$i] + $array2[$i];
//printing the new array
foreach ($array3 as $a3)
echo ("$a3, ");

11
Associative Arrays
• In associative Arrays, you can reference the items in an array
by name rather than by number.
• For example:
$paper = array
(
'copier' => "Copier & Multipurpose",
'inkjet' => "Inkjet Printer",
'laser' => "Laser Printer",
'photo' => "Photographic Paper "
);
12
Associative Arrays
• Note: you can use single or double quotes in the indexes as
well as values.
• Printing array elements:
echo "Array elements are:<br>";
foreach ($paper as $p)
echo ("$p, ");

13
Access mechanism operator
•  use double arrow operator (=>) to access string index as well
as the element.
• For example:
foreach ($paper as $p => $d)
echo ("$p: $d<br>");

14
Multi-dimensional Arrays (1 of 2)
• Multidimensional array is the ability to include an entire
array as a part of another one, and to be able to keep doing
so.
• A two-dimensional array is an array of array (a three-
dimensional array is an array of array of array).
• 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.

15
Multi-dimensional Arrays (2 of 2)
• However, arrays more than three levels deep are hard to
manage for most people.
• 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.

16
Two-dimensional Numerical Indexed
$student = array (
array ("Mohamed", 1990, "Hodan"),
array ("Ahmed", 2001, "Yaaqshiid"),
array ("Jaamac", 1986, "Shangaani"),
array ("Faadima", 2000, "Shibbis")
);
foreach($student as $s){
foreach ($s as $i)
echo ($i. " ");
echo ("<br>");
} 17
Two-dimensional Associative Arrays (1 of 4)
$products = array
(
'paper' => array(
'copier' => "Copier & Multipurpose",
'inkjet' => "Inkjet Printer",
'laser' => "Laser Printer",
),

18
Using Array Functions (1 of 2)
• is_array: Checks whether a variable is an array.
• in_array: Checks if a specified value exists in an array.
• count: Returns the number of elements in an array. sizeof
function behaves like count.
• sort: Sorts an indexed array in ascending order
• rsort: Sorts an indexed array in descending order
• max: Returns the highest value in an array, or several
specified values.
• min: Returns the lowest value in an array, or several specified
values. 19
Using Array Functions (2 of 2)
• shuffle: Shuffles an array and put elements in random order.
• explode: Place a string containing several words separated by
a single character (e.g. space) into an array.
• array_merge: Merges one or more arrays into one array.
• array_reverse: Return an array in the reverse order.

20
End of
Chapter 4
21

You might also like