Home  >  Article  >  Backend Development  >  How to create array in php function

How to create array in php function

PHPz
PHPzOriginal
2023-05-19 19:01:06549browse

In PHP, array is a very important data structure that can store multiple values. These values ​​can be different data types, such as integers, strings, objects, etc. Creating arrays is one of the basic knowledge of PHP. This article will introduce in detail the methods and techniques of creating arrays in PHP functions.

1. Use the array() function to create an array

The array() function is one of the most commonly used methods to create an array in PHP. Its syntax is as follows:

array(value1, value2, value3, ...);

Among them, value1, value2, value3, etc. represent the values ​​of array elements, separated by commas. Here is an example:

$arr = array("apple", "banana", "orange");

This array contains three elements, namely "apple", "banana" and "orange".

In addition to using strings as the values ​​of array elements, you can also use numbers, Boolean values, null values, etc. The following is an example of a mixed-type array:

$arr = array("apple", 123, true, null);

When using the array() function to create an array, you can also use key-value pairs to specify the key name and key value of the array elements, which makes it easier to access the array elements. . The key name can be an integer or a string. Use the "=>" symbol to separate the key name and key value. The following is an example of an array of key-value pairs:

$arr = array("name"=>"张三", "age"=>20, "gender"=>"男");

This array contains three elements, namely "name", "age" and "gender". Their key values ​​are "Zhang San", 20 and "Male" respectively.

2. Use the [] symbol to create an array

In addition to using the array() function, you can also use the [] symbol to create an array. This method is supported in PHP5.4 and above. An example is as follows:

$arr = ["apple", "banana", "orange"];

This array is equivalent to the array created using the array() function above.

When using the [] symbol to create an array, you can also specify the key name of the array element. The following is an example:

$arr = ["name"=>"张三", "age"=>20, "gender"=>"男"];

This array is equivalent to the array of key-value pairs created using the array() function above.

3. Use the range() function to create an array

The range() function can quickly generate a continuous array of integers. Its syntax is as follows:

range(start, end, step);

Among them, start represents the starting value of the array, end represents the end value of the array, and step represents the step size between array elements (default is 1).

The following is an example of an integer array from 1 to 10:

$arr = range(1, 10);

This array contains 10 elements, which are 1, 2, 3, 4, 5, 6, 7, 8 ,9,10.

4. Use the list() function to create an array

The list() function can assign values ​​to multiple variables at the same time. Its syntax is as follows:

list(var1, var2, var3, ...);

Among them, var1, var2 , var3, etc. represent variable names, separated by commas. You can use the list() function to convert an index array into multiple variables. The following is an example:

$arr = ["apple", "banana", "orange"];
list($a, $b, $c) = $arr;
echo $a; // 输出"apple"
echo $b; // 输出"banana"
echo $c; // 输出"orange"

This code assigns the elements in the array $arr to variables $a, $b, $c respectively. .

5. Use loops to create arrays

Use loops to create arrays dynamically. The following is an example of using a for loop to create an array:

$arr = [];
for ($i = 1; $i <= 10; $i++) {
    $arr[] = $i;
}

This array contains 10 elements, namely 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.

Note that when using a loop to create an array, you must first define an empty array, and then use the [] symbol or the array_push() function to add elements to the array.

Summary

This article introduces five ways to create arrays in PHP functions: using the array() function, [] symbol, range() function, list() function, and loops. Different methods are suitable for different scenarios, and the appropriate method should be selected according to the specific situation. Only by mastering the creation method of array can you process data more flexibly.

The above is the detailed content of How to create array in php function. 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