Home  >  Article  >  Backend Development  >  How to add elements to an array in php

How to add elements to an array in php

PHPz
PHPzOriginal
2023-04-25 09:06:201507browse

In PHP, an array is a very commonly used data structure. It can store a series of values, and the corresponding values ​​can be accessed according to the key name. In practical applications, we sometimes need to add new elements to existing arrays. This article will introduce how to add new elements to arrays in PHP.

Method 1: Directly assign a value to the array

The simplest method is to directly assign a value to the array. This method is suitable for adding a new element or replacing the value of an existing element. We can use the key name of the array as a subscript to assign a value to the array. The code is as follows:

In the above code, we first define an array $fruits containing three elements, and then we pass $fruits ["watermelon"] = "red"; This line of code adds a new element "watermelon" to the array. Finally we use the var_dump() function to output the value of the entire array. After executing the code, the output result is as follows:

array(4) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(6) "orange"
  ["watermelon"]=>
  string(3) "red"
}

You can see that the "watermelon" element has been successfully added to the array $fruits.

If we want to replace the value of an existing element, we only need to use the existing key name in the same way, for example:

In the above code, we will $fruits array The value of the second element (index 1) is replaced from "banana" to "kiwi". The output result is as follows:

array(3) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(4) "kiwi"
  [2]=>
  string(6) "orange"
}

Method 2: Use array_push() and array_unshift() functions

In addition to the above direct assignment method, we can also use some built-in functions provided by PHP to add array elements . Among them, the most commonly used are array_push() and array_unshift() functions.

array_push() function can add new elements to the end of the array, the code is as follows:

In the above code, we first define an array $fruits containing three elements, and then we use array_push() adds the new element "watermelon" to the end of the $fruits array. Finally we use the var_dump() function to output the value of the entire array. After executing the code, the output result is as follows:

array(4) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(6) "orange"
  [3]=>
  string(10) "watermelon"
}

You can see that the new element "watermelon" has been successfully added to the end of the array $fruits.

Corresponding to the array_push() function is the array_unshift() function, which can add new elements to the head of the array. The code is as follows:

In the above code, we first define a An array $fruits containing three elements, then we use array_unshift() to add a new element "watermelon" to the head of the $fruits array. Finally we use the var_dump() function to output the value of the entire array. After executing the code, the output result is as follows:

array(4) {
  [0]=>
  string(10) "watermelon"
  [1]=>
  string(5) "apple"
  [2]=>
  string(6) "banana"
  [3]=>
  string(6) "orange"
}

You can see that the new element "watermelon" has been successfully added to the head of the array $fruits.

Method 3: Use the " " operator

In addition to the above two methods, we can also use the " " operator to merge the original array and the new elements into a new array, code As follows:

In the above code, we first define an array $fruits containing three elements and an array $new_fruit containing one element, and then we use the " " operator to merge the two arrays Became a new array $result. Finally we use the var_dump() function to output the value of the entire array. After executing the code, the output result is as follows:

array(4) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(6) "orange"
  [3]=>
  string(10) "watermelon"
}

You can see that the new element "watermelon" has been successfully added to the end of the array $fruits.

Summary

This article introduces three common methods of adding elements to an array in PHP: directly assigning values ​​to the array, using the array_push() and array_unshift() functions, and using the " " operator. Each of these methods has its advantages and disadvantages, and we can choose the appropriate method according to the specific scenario. At the same time, when using these methods, you also need to pay attention to the key names and subscripts of the array to avoid errors.

The above is the detailed content of How to add elements to an array 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