How to add array items in php

PHPz
Release: 2023-04-18 15:42:06
Original
600 people have browsed it

As a web-oriented programming language, one of its characteristics is its rich array processing functions. For arrays, adding array items is one of the most basic and common operations. So, how to add array items in PHP? Next we will cover this issue in detail.

In PHP, there are two ways to add array items, namely: index array and associative array. In an indexed array, each array item has a unique index value, whereas in an associative array, each array item has a name as an index value. For these two different types of arrays, they are introduced as follows:

1. Index array

If we want to add a new array item to an existing index array, we can use The index value of this array is implemented by incrementing:

$array = array("apple", "banana", "orange");
$array[] = "pear";
Copy after login

After executing the above code, the $array array will become: array("apple", "banana", "orange", "pear" ).

If we want the added array item to be at a specified position, we can use the array_splice() function. The example is as follows:

$array = array("apple", "banana", "orange");
array_splice($array, 1, 0, "pear");
Copy after login

In the above code, the first element of array_splice() The first parameter is the array to be processed, the second parameter indicates the index position, the third parameter is the number of elements that need to be deleted, and the fourth parameter is the new element that needs to be added. After executing the above code, the $array array will become: array("apple", "pear", "banana", "orange").

2. Associative Array

If we want to add a new array item to an existing associative array, we can use the following two methods to achieve it:

  1. Direct assignment method
$array = array("name" => "Tom", "age" => 20);
$array["gender"] = "male";
Copy after login

After executing the above code, the $array array will become: array("name" => "Tom", "age" => 20, "gender" => "male").

  1. array_merge() function
$array1 = array("name" => "Tom", "age" => 20);
$array2 = array("gender" => "male");
$array = array_merge($array1, $array2);
Copy after login

After executing the above code, the $array array will become: array("name" => "Tom", "age" => 20, "gender" => "male").

It should be noted that if the name of the new array item already exists, the new value added using the above method will overwrite the existing value. If we want to retain these existing values, we can use the array_merge_recursive() function instead of the array_merge() function.

To sum up, the way to add array items in PHP is different depending on the array type. For indexed arrays, you can use the array increment or array_splice() function; for associative arrays, you can use the direct assignment method or the array_merge() function.

The above is the detailed content of How to add array items in php. For more information, please follow other related articles on the PHP Chinese website!

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