Home  >  Article  >  Backend Development  >  How to add specified elements to php array

How to add specified elements to php array

PHPz
PHPzOriginal
2023-04-25 10:32:49517browse

In PHP, array is a very important data type. It can be used to store a series of values ​​and access these values ​​through an index or associated key. In PHP, we can easily add new elements to an array and modify or delete existing elements. So, in this article, we will introduce how to add specified elements to a PHP array.

First of all, we need to understand the basic knowledge of arrays in PHP.

There are two types of PHP arrays: index arrays and associative arrays.

Indexed array refers to an array that uses numerical indexes as array elements, where the numerical index starts from 0 and increases in the order in which the elements are added. For example:

$numbers = array(1, 2, 3, 4, 5);

In the above code, $numbers is an index array containing 5 elements.

Associative array refers to an array that uses string keys as array elements, where the string keys can be any characters. For example:

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

In the above code, $person is an associative array, which contains 3 elements, namely "name", "age" and "gender".

Now, we will focus on how to add specified elements to a PHP array.

First, let’s take a look at how to add specified elements to an index array.

1. Use the [] operator to add elements

To add elements to the indexed array, you can use the [] operator, which adds a new element at the end of the array. For example:

$fruits = array("apple", "banana");
$length = count($fruits);
$fruits[$length] = "orange";
print_r($fruits);

In the above code, we first define an index array $fruits containing two elements: "apple" and "banana". Then, we use the count() function to get the length of the array, which is 2. Next, we added a new element "orange" at the last position of the array using the [] operator. Finally, we use the print_r() function to output the contents of the new array.

The output result is as follows:

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

As can be seen from the output result, the new element "orange" has been successfully added to the end of the array.

2. Use the array_push() function to add elements

In addition to using the [] operator, we can also use the array_push() function to add elements to the index array. The array_push() function adds a new element to the end of the array and returns the length of the new array. For example:

$fruits = array("apple", "banana");
$newLength = array_push($fruits, "orange");
print_r($fruits);
echo $newLength;

In the above code, we first define an index array $fruits containing two elements: "apple" and "banana". Then, we added a new element "orange" to the array using the array_push() function. Since the array_push() function returns the length of the new array, we store it in the $newLength variable. Finally, we use the print_r() function to output the contents of the new array, and use the echo statement to output the length of the new array.

The output result is as follows:

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

As can be seen from the output result, the new element "orange" has been successfully added to the end of the array, and the length of the new array is 3.

Next, let’s take a look at how to add specified elements to an associative array.

1. Use the [$key] operator to add elements

To add elements to an associative array, you can use the [$key] operator, where $key refers to a string key . For example:

$person = array("name" => "张三", "age" => 20);
$person["gender"] = "男";
print_r($person);

In the above code, we first define an associative array $person containing two elements: "name" and "age". Then, we use the [$key] operator to add a new element "gender" to the array and assign it the value "male". Finally, we use the print_r() function to output the contents of the new array.

The output result is as follows:

Array
(
    [name] => 张三
    [age] => 20
    [gender] => 男
)

As can be seen from the output result, the new element "gender" has been successfully added to the array.

2. Use the array_merge() function to add elements

In addition to using the [$key] operator, we can also use the array_merge() function to add elements to an associative array. The array_merge() function merges one or more arrays into a new array and returns the new array. For example:

$person = array("name" => "张三", "age" => 20);
$newPerson = array_merge($person, array("gender" => "男"));
print_r($newPerson);

In the above code, we first define an associative array $person containing two elements: "name" and "age". We then use the array_merge() function to merge $person and a new associative array (containing the "gender" element) into a new array $newPerson. Finally, we use the print_r() function to output the contents of the new array.

The output result is as follows:

Array
(
    [name] => 张三
    [age] => 20
    [gender] => 男
)

As can be seen from the output result, the new element "gender" has been successfully added to the array.

Summary

In this article, we introduced how to add specified elements to a PHP array. Specifically, we looked at how to add elements to indexed arrays and associative arrays, as well as adding elements to indexed arrays using the [] operator and the array_push() function, and to associative arrays using the [$key] operator and the array_merge() function. Add elements, etc.

In general, adding specified elements to a PHP array is very simple, as long as you master the corresponding syntax and functions. I hope this article can be helpful to everyone.

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