The function that defines an array in PHP is "array()". This function has two different forms: 1. Definition of index array. Each element of the array is assigned a unique index, starting from 0 Increasingly, the elements in the array can be accessed and modified through index; 2. The definition of associative array, each element of the array has an associated key "(key)", which can be used to access and modify the corresponding value.
The operating environment of this article: Windows 10 system, php8.1.3 version, dell g3 computer.
In PHP, the function that defines an array is array(). This function combines a set of values together into an ordered list. Arrays are a very common and useful data structure that can be used to store and manipulate multiple values.
The array() function can use two different syntax forms to define arrays. These two forms will be introduced below:
1. Definition of index array:
$array = array(value1, value2, value3, ...);
In this form, each element of the array is assigned a unique The index of , starting from 0 and increasing. Elements in an array can be accessed and modified through indexing.
For example:
$fruits = array("apple", "banana", "orange");
2. Definition of associative array:
$array = array(key1 => value1, key2 => value2, key3 => value3, ...);
In this form, each element of the array has An associated key that can be used to access and modify the corresponding value.
For example:
$student = array("name" => "John", "age" => 20, "class" => "B");
In addition to using the array() function to define an array, you can also use the shortcut [] syntax to define an index array. This is possible in PHP version 5.4 and above.
For example:
$fruits = ["apple", "banana", "orange"];
Whether you use the array() function or the [] syntax, you can use it to define an array. In subsequent code, you can use the same way to access and Operate elements in an array.
In summary, the function that defines an array in PHP is array(). This function in its two forms can be used to define indexed arrays and associative arrays.
The above is the detailed content of What is the function to define arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!