Home  >  Article  >  Backend Development  >  How to assign values ​​to array elements in php

How to assign values ​​to array elements in php

王林
王林Original
2023-05-19 09:36:07715browse

In PHP, array is a very common data structure. The elements of an array can be any type of data: numbers, strings, or even another array.

Assigning values ​​to array elements in PHP is very simple. You only need to call the array variable and element index, and then assign the value to the element. Here is an example:

// 定义一个数组
$arr = array("apple", "banana", "orange");

// 给索引为0的元素赋值
$arr[0] = "pear";

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

In this example, we first define an array with three elements. Then, we assign the value "pear" to the element with index 0 (by default, the first element has index 0). Finally, we output the entire array using the print_r() function. Run this script and you should see the following output:

Array
(
    [0] => pear
    [1] => banana
    [2] => orange
)

As you can see, the value of the first element has changed to "pear".

We can also use another way to assign values ​​to the elements in the array, that is, use PHP's list() function to assign values ​​to multiple elements at once. Here is an example:

//定义包含两个元素的数组
$arr = array("apple", "banana");

//用list()函数为数组元素分配值
list($arr[0], $arr[1]) = array("orange", "pear");

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

In this example, we initially define an array with two elements. We then use the list() function with another array containing two elements to assign new values ​​to the elements of this array. Finally, we use the print_r() function to output the elements of this array. Run this script and you should see the following output:

Array
(
    [0] => orange
    [1] => pear
)

As you can see, we successfully assigned a new value to both elements of the array.

In addition to using array indexes to assign values ​​to elements, we can also use PHP's array_push() function to add a new element to the end of the array. Here is an example:

//定义一个包含两个元素的数组
$arr = array("apple", "banana");

//push一个新元素到数组末尾
array_push($arr, "orange");

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

In this example, we initially define an array with two elements. We then added a new element to the end of the array using the array_push() function. Finally, we use the print_r() function to output the elements of this array. Run this script and you should see the following output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)

As you can see, we successfully added a new element to the end of the array.

In general, assigning values ​​to array elements in PHP is a very intuitive and simple thing. Whether you assign values ​​by index, assign values ​​using the list() function, or add new elements using the array_push() function, you can freely manipulate the array in your program and store the values ​​you need.

The above is the detailed content of How to assign values ​​to array elements in php. 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