How to define an array: 1. Use the "array(value 1, value e2...)" statement; 2. Use "array(key1=>value 1,key2=>value 2.. ..)" statement; 3. Use the "$a[key1]=value 1;$a[key2]=value 2;..." statement; 4. Define it through square brackets "[]".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The array in PHP is actually an ordered mapping. A map is a type that associates values to keys. This type is optimized in many ways, so it can be treated as a real array, or list (vector), hash table (which is an implementation of map), dictionary, set, stack, queue and many more possibilities. Since the value of an array element can also be another array, tree structures and multidimensional arrays are also allowed.
Generally speaking, the definition methods are as follows:
Method 1:
$a=array(value1,value2,value4,value5,value6...);
Example:
<?php $array=array('a','b','c'); $array[]='simon'; print_r($array); ?>
The running results are as follows shown.
Array ( [0]=>a [1]=>b [2]=>c [3]=>simon )
Method 2:
$a=array(key1=>value1,key2=>value2,key3=>value3);
Method 3:
$a[key1]=value1; $a[key2]=value2;
Method 4: Passed Brackets [] define the array
You can write like this after version 5.4 of PHP, with the new array abbreviation syntax.
php version 5.3 and previous versions do not accept writing like this...
$data = [ 'start_time' => '123', 'end_time' =>'456' ];
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the ways to define arrays in php. For more information, please follow other related articles on the PHP Chinese website!