Professional Documents
Culture Documents
Arrays
Arrays and Multi dimensional
Arrays
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Table of Contents
1. Arrays in PHP
2. Array Manipulation
3. Multidimensional Arrays
2
Arrays in PHP
What are Arrays?
An array is a ordered sequence of elements
The order of the elements is fixed
Can get the current length (count($array))
In PHP arrays can change their size at runtime (add / delete)
Element of an array
0 1 2 3 4 Element
Array of 5 index
elements … … … … …
4
Creating Arrays
Initializing Arrays
There are several ways to initialize an array in PHP:
Using the array(elements) language construct:
$newArray = array(1, 2, 3); // [1, 2, 3]
6
Initializing Arrays – Examples
// Creating an empty array
$emptyArray = array();
// Clearing an array
$myArray = array();
7
Accessing Array Elements
Array elements are accessed by their key (index)
Using the [] operator
By default, elements are indexed from 0 to count($arr)-1
0 1 2 3 4
Apple Pear Peach Banana Melon
$months = array();
array_push($months, 'January', 'February', 'March');
$months[] = 'April';
// ['January', 'February', 'March', 'April']
10
Delete from Array
unset($array[$index]) – removes element at given position
Does NOT reorder indexes
$array = array(0, 1, 2, 3);
unset($array[2]); Indices remain
print_r($array); // prints the array unchanged
// Array ([0] => 0 [1] => 1 [3] => 3)
11
Delete / Insert in Array
array_splice($array, $startIndex, $length) – removes the
elements in the given range
$names = array('Maria', 'John', 'Richard', 'George');
array_splice($names, 1, 2); // ['Maria', 'George']
20
5
30 60 15 2 4
10
40
14
Problem: Print Array Elements
Enter array elements from html form - array of strings holding
numbers - comma separated
Print the array as follows
20
7
30 20 70 3 7 10
40 30 60 4 3 5 2 2
50 40 50 5 4
60
10
70
15
Multidimensional Arrays
Multidimensional Arrays
A multidimensional array is an array containing one or more arrays
Elements are accessed by double indexing: arr[][]
Element is in 'row' 0,
One main array 0 1 2 'column' 2,
whose elements
0 4 6 3 i.e. $arr[0][2]
are arrays
1 2 1 2 Each sub-array
6 7 9 contains its own
2 elements
17
Multidimensional Arrays – Example
Printing a matrix of 5 x 4 numbers:
$rows = 5;
$cols = 4;
$count = 1;
$matrix = [];
for ($r = 0; $r < $rows; $r++) {
$matrix[$r] = [];
for ($c = 0; $c < $cols; $c++) {
$matrix[$r][$c] = $count++;
}
}
print_r($matrix);
18
Problem: Biggest Element in Matrix
A matrix of numbers comes as array of strings
Each string holds numbers (space separated)
3 5 17 12 91 5 3 5 7 12
-1 7 4 33 6 22 -1 4 33 2 20 50 10
1 8 99 3 10 43 8 3 0 4 8 33 145
99 33 145
19
Solution: Biggest Element in Matrix
//TODO
$matrix = [[1,3,4],[7,6,14],[23,67,89]];
$biggestNum = $matrix[0][0];
foreach ($matrix as $row) { 1 3 4
7 6 14
foreach ($row as $column) { 23 67 89
if ($column > $biggestNum) {
$biggestNum = $column;
}
}
}
echo $biggestNum;
20
Problem: Biggest and Smallest Element in
Matrix
A matrix of numbers comes as array of strings from input form
Each string holds numbers (space separated)
99 -1 33 -1 145 8
21
PHP - Sort Functions For Arrays
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the
value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the
value
krsort() - sort associative arrays in descending order, according to the
key
22
Sort Array in Ascending Order - sort()
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>
BMW
Toyota
Volvo
23
Sort Array in Descending Order - rsort()
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>
Volvo
Toyota
BMW
24
Sort Array (Ascending Order), According to
Value - asort()
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
25
Sort Array (Ascending Order), According to Key -
ksort()
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35
26
Sort Array (Descending Order), According to
Value - arsort()
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>
Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35
27
Sort Array (Descending Order), According to Key
- krsort()
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37
28