This article mainly introduces the method of adding elements to php arrays. The example summarizes the common methods of adding elements to arrays, which has some reference value. For more information, see
This article mainly introduces the method of adding elements to php arrays. The example summarizes the common methods of adding elements to arrays, which has some reference value. For more information, see
This article summarizes in detail how to add elements to php arrays. Share it with you for your reference. The specific analysis is as follows:
If we add array elements to a one-dimensional array, we can use ArrayListay_push. Of course, in addition to this method, we also have a more direct method. Here we will sort it out for you.
Add elements to a one-dimensional array
The Code is as follows:
$ ArrayList = ArrayListay ();
Array_push ($ ArrayList, el1, el2... eln );
But there is actually a more direct and convenient method, the Code is as follows:
The Code is as follows:
$ ArrayList = ArrayListay ();
$ ArrayList [] = el1;
$ ArrayList [] = el2;
...
$ ArrayList [] = eln;
The two methods have the same effect.
Add one or more elements. The Code is as follows:
The Code is as follows:
<? Php
$ ArrayList1 = ArrayListay ("a", "B ");
Array_push ($ ArrayList1, "c", "d ");
Print_r ($ ArrayList1 );
?>
The running result is as follows:
The Code is as follows:
ArrayListay
(
[0] =>
[1] => B
[2] => c
[3] => d
)
Note: If the first parameter is not an array, ArrayListay_push () will issue a warning.
Insert elements at the beginning of the array
1. ArrayListay_unshift. Use the following code:
The Code is as follows:
<? Php
$ Array_Qlist = ArrayListay ("", "banana ");
Array_unshift ($ array_Qlist, "watermelon", "foot home ");
Print_r ($ array_Qlist );
?>
The output is as follows:
The Code is as follows:
ArrayListay
(
[0] => Watermelon
[1] => foot home
[2] => Joint Military and Civil Defense
[3] => banana
)
The use of ArrayListay_push or ArrayListay_unshift to add correlated elements to an array does not work. How can we add them? Use the ArrayListay_merge method to add elements like ArrayListay_unshift at the beginning. The Code is as follows:
The Code is as follows:
<? Php
$ Array_Qlist = ArrayListay ('A', 'B ');
$ Array_Qlist = Array_merge (ArrayListay ('front '=> ''), $ array_Qlist );
/*
ArrayListay
(
[Front] => hello
[0] =>
[1] => B
)
*/
?>
2. + operator, the Code is as follows:
The Code is as follows:
<? Php
$ Array_Qlist = Array ('A', 'B ');
$ Array_Qlist = Array ('front' => 'Hello little ') + $ array_Qlist;
?>
The output result is the same as that of the ArrayListay_merge method.
3. Add the joined array element at the end of the element. The Code is as follows:
The Code is as follows:
<? Php
$ Array_Qlist = ArrayListay ('A', 'B ');
$ Array_Qlist ['front'] = '';
/*
Output
ArrayListay
(
[0] =>
[1] => B
[Front] => hello
)
*/
?>
As for some friends, the performance of adding elements to Array_push is almost the same as that of $ a [] = 'A'. If one value is used, it is incomparable.
I hope this article will help you with PHP programming.
,