Home > Article > Backend Development > Introduction to PHP functions: array() function
Introduction to PHP functions: array() function and sample code
Introduction:
In PHP, the array() function is a very commonly used function. Use to create an array. It can receive any number of parameters and create an array based on the values of the parameters. In this article, I will introduce you to the usage of the array() function and sample code to help you better understand and use it.
Function overview:
array() function is one of PHP's built-in functions, used to create an array. It can receive any number of parameters and create an indexed or associative array based on the values of the parameters. I will introduce the usage of creating indexed arrays and associative arrays respectively.
Create an index array:
The index array is the default type of array and the most commonly used type of array. Creating an indexed array is one of the simplest uses. You only need to pass the parameter values directly into the array() function. Here is an example:
$names = array("John", "Jane", "David", "Alice");
In the above example, we created an index array named $names, which contains 4 elements. The values of each element are "John", "Jane", "David" and "Alice" respectively. At this time, the value of $names[0] is "John", the value of $names[1] is "Jane", and so on.
Create an associative array:
An associative array is an array with custom key names, and each key name is associated with the corresponding value. We can use the => operator to separate the key names and values and pass them as arguments to the array() function. Here is an example:
$person = array("name" => "John", "age" => 25, "gender" => "male");
In the above example, we created an associative array named $person. The array contains three key-value pairs, namely "name" => "John", "age" => 25 and "gender" => "male". We can access and operate the corresponding value through the key name. For example, the value of $person["name"] is "John" and the value of $person["age"] is 25.
Notes:
When using the array() function, you need to pay attention to the following points:
Sample code:
The following is some sample code that uses the array() function to create an array to help you better understand and master its usage:
// 创建一个空数组 $emptyArray = array(); // 创建一个索引数组 $fruits = array("Apple", "Banana", "Orange"); // 创建一个关联数组 $person = array("name" => "John", "age" => 25, "gender" => "male"); // 创建一个包含索引数组和关联数组的混合数组 $mixedArray = array("Apple", "Banana", "color" => "Red", "size" => "Medium"); // 创建一个嵌套数组 $nestedArray = array(array("Apple", "Banana"), array("Orange", "Grape")); // 访问数组中的元素 echo $fruits[1]; // 输出: Banana echo $person["name"]; // 输出: John
Summary :
In this article, I introduced you to the array() function in PHP, which is a commonly used function for creating arrays. Whether you are creating an indexed array or an associative array, using the array() function is very simple. Through the display of sample code, I hope it can help you better understand and master the usage of the array() function. In actual PHP development, proficient use of the array() function will bring great convenience to your work.
The above is the detailed content of Introduction to PHP functions: array() function. For more information, please follow other related articles on the PHP Chinese website!