How to add key name and key value to PHP array

WBOY
Release: 2023-07-10 09:00:01
Original
1936 people have browsed it

How to add key names and key values ​​​​in PHP arrays

When we use PHP to program, we often need to operate arrays. One of the common operations is to add new key names and corresponding values ​​​​in the array. key value. In this article, we will learn how to implement this functionality through PHP code.

First, we need to create an empty PHP array. An empty array can be created using the following code:

$array = array();
Copy after login

Now, we have created an empty array named $array. Next, we can add new key names and key values ​​to the array through the following different methods.

Method 1: Use square brackets to add key-value pairs

Using the square bracket operator ([]) can easily add new key names and key values ​​to the array.

$array['name'] = 'John';
$array['age'] = 25;
Copy after login

The above code will add two key names and corresponding key values ​​​​in the $array array. The key names are 'name' and 'age' respectively, and the key values ​​are 'John' and 25 respectively.

Method 2: Add key-value pairs through the array_push() function

The array_push() function can add one or more elements to the end of the array. This function can be used when we want to add multiple key-value pairs to the array at the same time.

array_push($array, 'country', 'USA');
Copy after login

The above code will add two key-value pairs to the $array array. The key name is 'country' and the key value is 'USA'.

Method 3: Use the [] operator to add multiple key-value pairs at the same time

If we want to add multiple key-value pairs to the array at one time, we can use the [] operator.

$array += [
    'occupation' => 'developer',
    'hobby' => 'photography'
];
Copy after login

The above code adds two key names and key values ​​to the $array array at one time through the = operator. The key names are 'occupation' and 'hobby', and the key values ​​are 'developer' and 'photography' respectively.

Method 4: Use the array_merge() function to merge arrays

When we have two or more arrays, we can use the array_merge() function to merge them into one array and add a new one Key name and key value.

$array1 = array('name' => 'Tom', 'age' => 30);
$array2 = array('country' => 'China');
$array = array_merge($array1, $array2);
Copy after login

The above code merges the arrays $array1 and $array2 into one array, and assigns the new array to $array. The merged $array array contains the key-value pairs of the two original arrays.

Through the above four methods, we can flexibly add new key names and corresponding key values ​​​​in the PHP array. According to actual needs, just choose the appropriate method to operate.

Summary:

There are many ways to add key names and key values ​​to arrays in PHP. Through the square bracket operator, array_push() function, [] operator and array_merge() function we This function can be easily implemented. According to the specific needs, choose the corresponding method to add key names and key values. I hope the sample code in this article can help readers better understand how to add key names and key values ​​​​in PHP arrays.

The above is the detailed content of How to add key name and key value to PHP array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template