Home > Backend Development > PHP Problem > php creates a new array and adds elements

php creates a new array and adds elements

王林
Release: 2023-05-06 11:29:09
Original
613 people have browsed it

In PHP, an array is a very common data type and a container that holds a series of ordered data. Arrays are very useful when dealing with large amounts of data, and they are also very practical because they can expand the container by adding new elements. This article will introduce how to create a new array and add elements in PHP.

  1. Create a new array

There are usually two ways to create a new array. The first is to manually create an array through the keyword array, as shown below:

$fruits = array();
Copy after login

The above code will create a new array named $fruits, It contains no elements. We can also initialize the elements while creating the array, as shown below:

$fruits = array('apple', 'banana', 'cherry');
Copy after login

In the above code, we not only create a new array named $fruits, but also initialize 3 elements.

Another way to create an array is to use square brackets ([]) to create an empty array, as shown below:

$fruits = [];
Copy after login

This method is the same as the first Both methods are essentially the same, with slightly different syntax.

  1. Add elements to array

Once we have created a new array, we can add elements to it using the built-in array_push() function . array_push()The function will add an element to the end of the array, and can add multiple elements at once. The following is a sample code:

$fruits = array(); // 创建一个新数组

// 将元素添加到数组中
array_push($fruits, 'apple', 'banana', 'cherry');

// 在屏幕上输出数组
print_r($fruits);
Copy after login

In the above code, we first create a new array $fruits, and then use the array_push() function to Three elements are added at the end: apple, banana, and cherry. Finally, we use the print_r() function to output the entire array structure on the screen.

In addition, we can also directly add new elements to the end of the array, as shown below:

$fruits = array(); // 创建一个新数组

// 将元素添加到数组中
$fruits[] = 'apple';
$fruits[] = 'banana';
$fruits[] = 'cherry';

// 在屏幕上输出数组
print_r($fruits);
Copy after login

In the above code, we use empty square brackets ([]) Three elements are added to the end of the array. This method has the same effect as using the array_push() function, but is more convenient when only adding one element.

In both the above cases, we can create a new element in the array and then add it at the end of the array. However, if you want to insert a new element at a specific position in the array, you must use another method, which is to insert the element in the middle of the array and move the existing elements to make room for the new element. The specific method is as follows:

$fruits = array('apple', 'banana', 'cherry'); // 创建一个新数组

// 将元素插入到数组中
array_splice($fruits, 1, 0, 'grape');

// 在屏幕上输出数组
print_r($fruits);
Copy after login

In the above code, we use the array_splice() function to insert the new element grape into the $fruits array of the second position. When using this function, you first need the array as the first parameter, then the index position to insert (the second parameter), then the number of elements to delete (0 in this case), and finally the element to insert. After running the code, we can see the results as follows:

Array
(
    [0] => apple
    [1] => grape
    [2] => banana
    [3] => cherry
)
Copy after login

In the above example, we successfully inserted the element grape into the second position of the $fruits array location. You can also insert the element at any other position, just change the second parameter.

  1. Conclusion

In this article, you have learned how to create and initialize a new array and add elements to it using a variety of methods. Of course, the above only covers part of PHP arrays. In actual work, you need to understand the complete functions of PHP arrays so that you can better utilize arrays when processing data.

The above is the detailed content of php creates a new array and adds elements. 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