PHP - array
Trieda
Metóda - array
(PHP 4, PHP 5, PHP 7)
Creates a new array. This is not really a function but a language construct.
Note: Sice PHP 5.4 we can use the short array syntax:
$array = [];
PHP supports associative arrays. We can use almost everything as a key. When
we create an array, we can define its values and their keys or just the values.
If we don't set the keys, they're automatically generated as numeric indexes. We
use the => operator to assign a value to the key.
Note: We can use the print_r() function for
printing array contents when debugging.
Procedurálne
- function array (mixed $...) : array
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $... | mixed | The array definition. See the function description or the example. |
Mávratovej hodnoty
Vracia: array
Returns the array with the given values. We can access each element of the array in the following way:
$value = $array['someKey']; $value = $array[0];
Príklady
Creating a simple array with automatic keys (numeric index) + example of the short array syntax:
<?php
$array = array('John', 'Richard', 'Billy', 'David');
print_r($array);
$array = ['John', 'Richard', 'Billy', 'David']; // The short syntax, creates the same array as above
print_r($array);
Some (or all) of an array elements can have the key defined:
<?php $array = [ 'key1' => 'element1', 'key2' => 'element2', 'element3', 'element4' ]; print_r($array);
We can define an array as an array's value (when an array contains another array, we call the outer array multidimensional):
<?php $array = [ 'subarray1' => [ 'sub-element1', 'key2' => 'element2' ], [ 'sub-element1', 'key2' => 'element3' ] ]; print_r($array);
Súvisiace manuály
- function count (mixed $array_or_countable, int $mode = COUNT_NORMAL) : int
