Chapter 2-4 PHP Arrays
Chapter 2-4 PHP Arrays
Chapter 2-4 PHP Arrays
CHAPTER 2:
PHP Arrays (4 of 5)
Topics covered:-
Arrays in PHP
Accessing elements of an array
Using loop to access array elements
Creating multidimensional arrays
Using functions to manipulate arrays – merging, sorting
and etc.
Variables vs Arrays
• Recall that a variable is a container that holds a value.
• Unfortunately, variables only allows to hold a value. If you use the same
variable to declare many values, only the last value declared will be
registered and recognized by that variable name.
• Imagine you would like to store more than one value referenced by a
single name, a variable is not feasible to be used.
• Solution:-
Declare, initialize and use array instead.
Introduction to Arrays
• Use array to work with a large amount of data.
• The above situation can be easily solved by just declaring an array name
called $product that holds information of all product at once.
Types of Arrays
• PHP supports two types of arrays:-
• Index arrays
• Associative arrays
$studentName $testMark
[5] Herman Herman 100
Indexed Array
• These are arrays where each element is referenced by a numeric key /
index.
• The first index / key starts from zero (0).
• The second index / key is one (1) and so on…..
Index/Key Element / Value
0 23.50
1 40.00
2 100.99
3 56.99
4 88.20
5 97.25
• The above array is of size 6 and store a list of product prices of type
double / float.
AMIT 2043 Web Systems and Technologies
PHP Arrays (4 of 5) Slide 6
Associative Array
• This type of array is also called a hash / map.
• Instead of numeric key indexes, associative array is referenced by a
string index.
Index/Key Element / Value
jimmyChoo 590.00
louisVuitton 1200.00
prada 100.99
valentino 475.00
gucci 88.20
chloe 97.00
• The above array is of size 6 and store a list of handbag prices of type
double / float.
Note: The original array left untouched. However, the index of Bernard and
Candy changes from [1] and [2] to [0] and [1] instead.
In order to preserve the original index of Bernard and Candy, use the optional
fourth argument, like:-
print_r(array_slice($members,
AMIT 2043 Web Systems and Technologies 1, 2, true));
PHP Arrays (4 of 5) Slide 11
current() Returns the value of the current element pointed by the pointer, without changing the pointer
position.
key() Returns the index of the current element pointed to by the pointer, without changing the pointer
position.
next() Moves the pointer forward to the next element, and returns that element’s value.
prev() Moves the pointer backward to the previous element, and returns that element’s value.
end() Moves the pointer to the last element in the array, and returns that element’s value.
reset() Moves the pointer to the first element in the array, and returns that element’s value.
NOTE: Recalled that when a function returned a 1 is referred to as true and all other
value returned is referred to as false.
foreach() Loop
• It is used when you want to travers the entire array sequentially from the
beginning of the array until to the end of the array.
• Syntax:
foreach(<array name>
as
<any variable to capture element’s value at each loop>){
// do something with each array element’s value}
• Example:-
foreach($members as $value){
echo $value; //just print each array element’s value
}
foreach() Loop
• The previous example only been able to access each array element’s value.
• Example:-
foreach($members as $key => $value){
//print each array index and its associated element’s value
echo $key . “ => ” . $value . “<br>”;
}
foreach() Loop
• If you wish to change each element’s value in the array in the foreach loop,
just pass the value as a reference parameter using the symbol ampersand (&).
• Syntax:
• Example:-
foreach($productPrice as &$value){
$value += 5; //add 5 to every product price
}
print_r($productPrice); //print all array elements to inspect changes
Multidimensional Array
• Below shows a two-dimensional array with 5 rows and 4 columns.
$numCar = 0; An array
foreach($car as $individualCar){
echo "Car #" . $numCar . "<br>";
echo "================<br>";
$numCar++;
Manipulating Arrays
• This section covers some common and useful array-processing functions.
Sorting
• Recall that array has two types:-
• Indexed Array
• Associative Array
• In order to perform sorting, PHP does provide sorting functions to treat
these two types of arrays.
• These sorting functions include:-
• sort() and rsort() – For sorting indexed array
• asort() and arsort() – For sorting associative array
• ksort() and krsort() – For sorting associative array by keys
• array_multisort() – sorting multiple arrays at once including multidimensional
arrays
rsort($members);
print_r($members);
• Resulted Output:
array_multisort() Function
• Can be used to sort more than one single-dimensional at the same time.
• Can also be used to sort multidimensional array.
• With the above syntax, array_multisort() function will sort all arrays
according to the first sorted array element’s values.
array_shift() Removes the first element from the start of the array
array_pop() Removes the last element from the end of the array
echo array_push($andriodCodeName,
"Gingerbread",
"Honeycomb",
"Ice cream Sandwich",
"Jelly Bean"); //returned 8
array_splice() function
• Recall that array_splice() function is to add/remove element at any
position in the array at the same time.
• Syntax:
array_splice(<array>, <start index>, <length>,
<array to add>) //returns an array
• Explanation:-
• If length is not specified, array_splice() will remove element from the
start index until the last element.
array_splice() function
• Example:-
$oriArray = array("Lake Toba", "Bali");
$addArray = array("Jakarta","Aceh");
print_r(array_splice($oriArray, 0)); //print empty array
print_r($oriArray); //print the array with new elements
• This function works well with indexed array but not useful working with
associative arrays.
• Why? The reason being that the associative array key are not retained when
new items are added. Instead, array_splice() function will remove the key of
that newly added element(s) and follow the original indexes.
array_splice() function
• Example:-
However, the above can be cut short and be done with list() function.