Summary of adding elements to php Arrays

Source: Internet
Author: User
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.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.