Home  >  Article  >  Backend Development  >  How to initialize php array

How to initialize php array

(*-*)浩
(*-*)浩Original
2019-09-26 14:09:324067browse

How to initialize php array

#An array in PHP is actually an ordered map. Mapping is a type that associates values ​​to keys.

This type has been optimized in many aspects, so it can be treated 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. (Recommended learning: PHP programming from entry to proficiency)

You can use the array() language structure to create a new array. It accepts any number of comma-separated key => value pairs.

There are generally two ways to initialize an array: one is to assign values ​​to the elements in the array individually; the other is to initialize all the elements together.

The following is a brief introduction to the two methods of initializing an array:

The first method:

$a["color"]="red";
$a["taste"]="sweet";
$a["shape"]="round";
$a["name"]="apple";
$a[3]=4;

The second method :

$a=array(
"color"=>"red",
"taste"=>"sweet",
"shape"=>"round",
"name"=>"apple",
3=>4);

The effects of these two methods are the same, the only difference is the method of assigning values ​​to elements.

The above is the detailed content of How to initialize php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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