Using the PHP function array() in order to create an array
- An array is a table of values which have been assigned to certain keys (a key is also called an "index").
- A key can be an integer or a string.
- The syntax to follow with the function array() is:
array(key1 => value1,...,keyn => valuen)
| <?php $my_array = array(1, TRUE, 3 => 'john', 5); print_r($my_array); ?> |
Remark:
The function print_r() displays the array in a human-readable format (print_r stands for "print readable").
Run the PHP script in your web browser:
Explanation:
- By default, in the absence of a key, the array function will choose the rank of the value in the array() declaration: for instance, TRUE is the second value declared, therefore it will automatically be assigned to the key 1 (remember that the first key is 0, and not 1).
- The value 'john' is assigned to the key 3. Afterwards, by default, in absence of explicit keys, the key 4 will be automatically chosen (because the last key assigned was key 3); the key 2 will therefore be left unassigned, unless you explicitly do it.
- When a value is explicitly assigned to a negative integer key (which is allowed), the next key to be assigned by default (if no explicit key is given) is the key 0.
Learn the PHP code:
<?php
$a = array(-5 => 3, 2, -4 => 4);
print_r($a);
?>
Run the PHP script in your web browser:
- If two different values have been assigned to the same key, the last value assigned will prevail.
- It is possible to use string keys at the same time as integer keys:
Learn the PHP code:
<?php
$my_array = array(1, TRUE, 3 => 'john', 'other' => 8, 5);
print_r($my_array);
?>
Run the PHP script in the web browser:
Remark:
Just like before, the 5th value assigned (which happens to be a 5) will be automatically assigned to the key 4 (since the last integer key to be assigned was 3).
The value of an array A associated with a given key n (respectively with a string key 'key') is designated by $A[n] (respectively by $A['key']).
Reciprocally, you can very well define your array by assigning values to keys on a case by case basis:
Reciprocally, you can very well define your array by assigning values to keys on a case by case basis:
Learn the PHP code:
| <?php $my_array = array(1, TRUE, 3 => 'john', 'other' => 8, 5); echo $my_array[3].'<br>'; $my_array['other']='new'; print_r($my_array); ?> |
Run the PHP script in your web browser:
PHP ARRAY AND ARRAY OF ARRAYS - PHP ARRAY OF ARRAYS
We have seen that an array is a map assigning values to keys. There is no restriction on the type of values: in particular, such values can be arrays themselves. In this case, we will say that we have an array of arrays, which roughly corresponds to a multidimensional table (matrix). The example below gives the example of a 2-dimensional PHP array which replicates the 3 x 3 multiplication table:
Learn the PHP code:
| <?php $my_array = array(1 => array(1 => 1, 2 => 2, 3 => 3), 2 => array(1 => 2, 2 => 4, 3 => 6), 3 => array(1 => 3, 2 => 6, 3 => 9)); echo '2 x 3 = '.$my_array[2][3]; ?> |
Run the PHP script in your web browser:
Remark:
By the same token, you can define an array of array of arrays, etc ... by induction.
PHP ARRAY AND ARRAY OF ARRAYS - PHP ARRAY FUNCTIONS
There exist a lot of array functions; we will introduce here a couple of them. The interested reader will be able to find the exhaustive list of PHP array functions in the PHP manual.
The sizeof function
sizeof($array) returns the number of elements contained in $array.
The implode function
The implode() function is a very useful one; it allows you to return the values of an array as a string, each of the values being separated by a string called the glue parameter (which can be a comma, for instance).
The syntax is given by implode($array,glue_parameter).
The syntax is given by implode($array,glue_parameter).
Learn the PHP code:
| <?php $array2 = array(1 => 'one', 2 => 'two'); $result = implode($array2,' , '); echo $result; ?> |
Run the PHP script in your web browser:
The explode function
The explode() function is the reciprocal function of implode(): given a string $string and a delimiter $delimiter (which is a string acting as a separator, for instance a comma), explode($string,$delimiter) will return an array composed of the substrings of $string which are delimited by the separator $delimiter.
Let's now try to reconstruct the array from the example above by using the explode() function.
Let's now try to reconstruct the array from the example above by using the explode() function.
Learn the PHP code:
| <?php $delimiter = ' , '; $string = 'one , two'; $result = explode($delimiter, $string); echo $result; ?> |
Run the PHP script in your web browser:
PHP ARRAY AND ARRAY OF ARRAYS - PHP ARRAY OPERATORS
You can use for arrays most of the operators that were defined in the previous tutorial (==, ===, !=, !==); you can also use the operator + which denotes the union of two arrays: $array1 + $array2 will transfer all the values of $array2 into $array1 (if two values are assigned to the same key, the value from $array1 will be preserved).
Learn the PHP code:
| <?php $array1 = array(1 => 'one'); $array2 = array(1 => 'other', 2 => 'two'); $array = $array1 + $array2; print_r($array); ?> |
Run the PHP script in your web browser:
Remark:
If instead of printing $array1 + $array2 you decide to print $array2 + $array1, the value assigned to key 1 will be 'other' instead of 'one'.
PHP arrays are a useful way to handle series of variables; they come with a lot of php array functions and operators which make such series very convenient to manipulate.
No comments:
Post a Comment