This is down to changes that were made for new versions of ZPE.
Put simply, an array is a collection of other items. In programming, an array can be seen to similar to the way that variables are stored in memory. In this sense, there is a direct mapping between the variable name or index and the value.
Arrays are incredibly useful ways of storing lots of information in one easy to access place. Not only this, unlike 1-dimension variables (all variables used in this tutorial), array values can be defined at runtime.
As mentioned, arrays can be seen as maps:
An array provides a map from the left (index or key) to something on the right (value)
In PHP, unlike certain other languages, arrays are dynamically sized meaning that the size can change at any point without having to redeclare the array (this may go on in the background in the PHP interpreter but the language itself does not require this).
An array in PHP is declared using the
<?php $array = array(); ?>
This defines an empty array that is, one with no elements within it. The array need not be empty on the instantiation of the array:
<?php $array = array("Firstname", "Surname"); ?>
PHP 5.4 also defines a new syntax that is similar to most other languages (including my own ZPE)
that does not require the use of the
<?php $array = ["Firstname", "Surname"]; ?>
PHP the use of the
array_push function to put an element in the array:
<?php $array = array(); array_push($array, "Value"); ?>
Arrays can also be given a value at a certain index using:
<?php $array("Firstname", "Surname"); ?>
Arrays can also be given mixed values in PHP, so an array that has a collection of string values can have integers added to it and vice versa.
Like many other languages, PHP defines a simple way of getting a value from an array. PHP uses the index (a numeric location in the array) or a key to retrieve a value from the array.
Using the index, the keys start at the number 0 and count up so the first index is 0.
<?php $array = array(); ... echo $array; ?>
This simple PHP script here will print to the screen the value of the array found at index 1.
An associative array is an array in which the key of the map is defined. By default, PHP arrays define a key using an index, but with associative arrays a key is defined, thus creating an association between the key and it's value.
The only difference between a standard array and an associative array is that keys are defined by the program.
Associatve arrays can be defined as being empty in the same way as a standard array. In the following case, an array has been defined which has the map of "Jack" to "Adams":
<?php $array = array("Jack" => "Adams"); ?>
Values can be added using the
array_push function but they must follow the same syntax as above:
<?php array_push($array, "Key" => "Value"); ?>
Elements can be retrieved the same way with associative arrays as with standard arrays except that instead of the index, the key is given:
<?php $array = array(); echo $array["Key"]; ?>
S1s and S2s really enjoyed the lesson on cybercrime using the lesson on How to Rob A Bank on… https://t.co/MxjDYE2JFu
3 months ago
S2s doing really well in hardware test! Also really enjoying doing tests electronically rather than on paper.… https://t.co/ritdfcW6Mw
3 months ago
S1 and S2 pupils listing input and output devices and giving answers such as keyboard and mouse but there were also… https://t.co/dGxDkYqFxo
3 months ago