PHP development
Lesson 5
Arrays
An array stores multiple values in one single variable.
array(3) {
[0]=>
string(6) "monkey"
[1]=>
string(5) "horse"
[2]=>
string(3) "cow"
}
array(3) {
[0]=>
string(6) "monkey"
[1]=>
string(5) "horse"
[2]=>
string(3) "cow"
}
array(3) {
[0]=>
string(6) "monkey"
[1]=>
string(5) "horse"
[2]=>
string(3) "cow"
}
associative array
array(3) {
["animal1"]=>
string(6) "monkey"
["animal2"]=>
string(5) "horse"
["animal3"]=>
string(3) "cow"
}
array(2) {
[0]=>
array(3) {
[0]=>
string(6) "monkey"
[1]=>
string(5) "horse"
[2]=>
string(3) "cow"
}
[1]=>
array(3) {
["animal1"]=>
string(6) "monkey"
["animal2"]=>
string(5) "horse"
["animal3"]=>
string(3) "cow"
}
}
Types of Arrays
There are three different kind of arrays and each array value is accessed using an ID, which is called array index.
Numeric/Indexed Array − An array with a numeric index. Values are stored and accessed in linear fashion.
Associative array − An array with strings as an index. This stores element values in association with key values rather than in a strict linear index order.
Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.
Control structures
https://www.php.net/manual/en/language.control-structures.php
for
foreach
animal1animal2animal3
0 = monkey
1 = horse
2 = cow
animal1 = monkey
animal2 = horse
animal3 = cow
monkey
Homework
create an array with 5 different fruits where every fruit has the taste 'sweet' or 'sour'.
create a form where you can make a choice for sweet or sour. If you submit the form all fruits with the choosen taste should be printed to screen.
like:
banana = sweet
mango = sweet
orange = sweet
or:
lemon = sour
tamarind = sour