Home > Article > Backend Development > php array definition
Definition Arrayarray()
You can use the array() language structure to create a new array. It accepts a number of comma-separated key => value parameter pairs.
array( [key =>]value , ... )// key can be integer or string// value can be any value
<?php$arr = array("foo" => "bar", 12 => true);echo $arr["foo"]; // barecho $arr[12]; // 1?>
PHP The array is actually an ordered map. A map is a type that associates values to keys. This type has been optimized in many aspects, so it can be regarded as a real array, or list (vector), hash table (an implementation of mapping), dictionary, set, stack, queue and more Many 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(1,2,4,5,6); <?php $array=array('a','b','c'); $array[]='simon'; print_r($array); ?>
The running results are as follows.
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: Define the array through square brackets []
This can be done after php version 5.4 Write, Add array abbreviation syntax.
php version 5.3 and previous versions do not accept writing like this...
$data = [ 'start_time' => '123', 'end_time' =>'456' ];
The above are 4 methods of defining arrays. You can try it yourself!
Related recommendations:
The most complete introduction to PHP arrays
php array replacement function array_replace()
Several ways to delete elements with specified values in php arrays
The above is the detailed content of php array definition. For more information, please follow other related articles on the PHP Chinese website!