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

Chapter 4 Arrays in PHP

This chapter discusses arrays in PHP. Arrays can hold multiple values under a single name and values can be accessed by index. Arrays can be numerically indexed or associative. Multidimensional arrays can hold arrays within arrays. Built-in functions allow adding/removing elements, sorting, and looping through arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Chapter 4 Arrays in PHP

This chapter discusses arrays in PHP. Arrays can hold multiple values under a single name and values can be accessed by index. Arrays can be numerically indexed or associative. Multidimensional arrays can hold arrays within arrays. Built-in functions allow adding/removing elements, sorting, and looping through arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter 4

Arrays in PHP
Introduction
• An array in PHP is a special variable, which can hold more
than one value at a time.
• An array can hold many values under a single name, and you can
access the values by referring to an index (a number or string).
• Each member of the array is called an element. 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 to the values within the array.
• Example:
$a = array (2, 5, 8, 3, 6.5, "Jamac", 4, "Ali");

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:
$a = array (
"Mohamed Abdullahi Abdi",
"Tifow",
24,
"Laba Dhagax",
);

7
• 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 ($a); $i++)
echo ("$a[$i], ");

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

9
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.
• When PHP encounters a foreach statement, it takes the first
item of the array and places it in the variable following the as
keyword; and each time control flow returns to the foreach,
the next array element is placed in the variable following the
as keyword. Once all values have been used, execution of the
loop ends.
10
Example
$numbers = array (26, 11, 13, -4, 14, 17, 5 , 52, 7, 9, 21, 32, 2,
4, 5);
foreach ($numbers as $n)
echo ("$n, ");

11
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");

Output: Total of all elements is: 214

12
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, ");
Output: 7, 9, 11, 13, 15,
13
Associative Arrays
• In associative Arrays, you can reference the items in an array
by name rather than by number. Use double arrow operator
(=>) to connect key/value (index/value).
• For example:
$assoc = array (
'name'=>"Mohamed Abdullahi Abdi",
'nickname'=>"Tifow", 'Age'=>24, 'Address'=>"Laba Dhagax",
'Income'=>432.5, 'gender'=>"male"
);

14
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 ($assoc as $a)
echo ("$a, ");

Output: Mohamed Abdullahi Abdi, Tifow, 24, Laba Dhagax, 432.5,


male,

15
Access mechanism operator
• Use double arrow operator (=>) to access string index as
well as the element.
• For example:
echo ("Printing key/value pairs: "); Output
foreach ($assoc as $k => $v) name: Mohamed Abdullahi Abdi
echo ("$k: $v<br>"); nickname: Tifow
Age: 24
Address: Laba Dhagax
Income: 432.5
gender: male
16
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. In fact, they can be as many dimensions as you like but
arrays more than three levels deep are hard to manage for
most people.
• 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 or
more sets of indexes, like coordinates on a map or graph.

17
Multi-dimensional Arrays (2 of 2)
• PHP understands multidimensional arrays that are two,
three, four, five, or more levels deep.
• 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.

18
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, ");
}
19
Two-dimensional Associative Arrays (1 of 4)
$products = array
(
'paper' => array (
'copier' => "Copier & Multipurpose",
'inkjet' => "Inkjet Printer",
'laser' => "Laser Printer",
),

20
Two-dimensional Associative Arrays (2 of 4)
'pens' => array (
'ball' => "Ball Point",
'hilite' => "Highlighters",
'marker' => "Markers"),
'misc' => array (
'tape' => "Sticky Tape",
'glue' => "Adhesives",
'clips' => "Paperclips")
);
21
Two-dimensional Associative Arrays (3 of 4)
foreach($products as $p){
foreach ($p as $i)
echo ("$i, ");
echo ("<br>");
}

22
Two-dimensional Associative Arrays (4 of 4)
• Use double arrow operator (=>) to access string index as
well as the element.
• For example:
foreach($products as $section => $items)
foreach($items as $key => $value)
echo ("[$section,$key]: ($value)<br>");
$section = 1st index
$key = 2nd index
$value = value
23
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 array in ascending order
• rsort: Sorts an array in descending order
• max: Returns the highest value in an array.
• min: Returns the lowest value in an array.

24
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.
• For example: $b = explode (" ", $a);
• array_merge: Merges one or more arrays into one array.
• array_reverse: Return an array in the reverse order.

25
End of Chapter 4

26

You might also like