Home > Backend Development > PHP Tutorial > php array definition

php array definition

肚皮会动
Release: 2023-03-17 12:52:01
Original
1875 people have browsed it

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?>
Copy after login

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(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;);
$array[]=&#39;simon&#39;;
print_r($array);
?>
Copy after login

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);
Copy after login

Method 3:

$a[key1]=value1;
$a[key2]=value2;
Copy after login

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 = [
&#39;start_time&#39; => &#39;123&#39;,
&#39;end_time&#39; =>&#39;456&#39;
];
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template