Home  >  Article  >  Backend Development  >  What are the two ways to assign values ​​to php arrays?

What are the two ways to assign values ​​to php arrays?

PHPz
PHPzOriginal
2023-04-18 14:09:05752browse

In PHP, we usually use arrays to store a set of data. There are two main ways to assign values ​​to arrays: ordinary assignment and reference assignment.

  1. Ordinary assignment

Ordinary assignment refers to assigning a value or expression directly to an array element. This assignment method is often used to create a static array or add a new value to an array. Add elements. The specific implementation syntax is: $array[index] = value, where index can be an integer or a string, indicating the key name of the array element, and value is the value or expression to be assigned.

The following is a sample code:

// 创建一个包含整数和字符串的数组
$arr = array(1, "hello", 3.14);

// 直接为数组新增元素
$arr[3] = true;
$arr["test"] = "world";

// 输出数组
print_r($arr);

In the above code, we first use the array() function to create an array containing three elements: integer 1, string "hello" and floating point number 3.14. Next, we add a Boolean value and a string element via $arr[3] = true and $arr["test"] = "world" respectively. Finally, we use the print_r() function to output the contents of the array. The output result is as follows:

Array
(
    [0] => 1
    [1] => hello
    [2] => 3.14
    [3] => 1
    [test] => world
)

We can see that the two newly added elements are represented by integers 3 and the string "test" are added to the array as key names.

  1. Reference assignment

Reference assignment (also called pass by reference) means that when the value of an array element is passed to a variable, the variable is not one of the values. copy, but points directly to the address of the element. This assignment method is often used to transfer large arrays or multiple nested arrays, which can improve program efficiency. The specific implementation syntax is: $var = &$array[index], where $var is the variable to be assigned, $array is the array name, index is the key name of the element to be retrieved, and & is the reference symbol in PHP.

The following is a sample code:

// 创建一个包含整数和字符串的数组
$arr = array(1, "hello", 3.14);

// 将数组元素的值赋给变量
$a = &$arr[0];
$b = &$arr[1];

// 修改变量的值
$a = 2;
$b = "world";

// 输出数组
print_r($arr);

In the above code, we first use the array() function to create an array containing three elements. Next, we assign the first and second elements of the array to # by $a = &$arr[0] and $b = &$arr[1] respectively. The two variables ##$a and $b. Next, we modify the values ​​of $a and $b and output the array. The output is as follows:

Array
(
    [0] => 2
    [1] => world
    [2] => 3.14
)
We can see that by modifying the values ​​of

$a and $b, the values ​​of the first and second elements of the array also change made corresponding changes.

To sum up, there are two ways to assign values ​​to arrays: ordinary assignment and reference assignment. For different application scenarios, we can choose different assignment methods to achieve higher efficiency and flexibility.

The above is the detailed content of What are the two ways to assign values ​​to php arrays?. 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