PHP Array add element method summary, PHP array element Summary _php tutorial

Source: Internet
Author: User

PHP Array add element method summary, PHP array elements Summary


This article summarizes the method of adding elements to the PHP array in detail. Share to everyone for your reference. The specific analysis is as follows:

If we are a one-dimensional array to add array elements we can use Arraylistay_push, of course, in addition to this method we have a more direct approach, here to arrange for you.

Adding elements to a one-dimensional array
Copy the Code code as follows: $ArrayList = Arraylistay ();
Array_push ($ArrayList, El1, El2 ... ELN);

But in fact there is a more direct and convenient approach, the code is as follows:
Copy the Code code as follows: $ArrayList = Arraylistay ();
$ArrayList [] = EL1;
$ArrayList [] = El2;
...
$ArrayList [] = ELN;
The effect of the two methods is the same.

Add one or more elements with the following code:
Copy the Code code as follows: <?php
$ArrayList 1 = Arraylistay ("A", "B");
Array_push ($ArrayList 1, "C", "D");
Print_r ($ArrayList 1);
?>
The results of the operation are as follows:
Copy the Code code as follows: Arraylistay
(
[0] = a
[1] = b
[2] = C
[3] = d
)
Note: If the first parameter is not an array, Arraylistay_push () will issue a warning.

Inserting an element at the beginning of an array

1. Arraylistay_unshift, using the method, the code is as follows:
Copy CodeThe code is as follows: <?php
$array _qlist = Arraylistay ("element", "banana");
Array_unshift ($array _qlist, "watermelon", "Home for the guests");
Print_r ($array _qlist);
?>

The output is as follows:
Copy CodeThe code is as follows: Arraylistay
(
[0] = Watermelon
[1] = home of the Helping guests
[2] = element
[3] = Banana
)
Array adding associative elements using Arraylistay_push or arraylistay_unshift is not going to work, so how do we add it, using the Arraylistay_merge method to implement a similar Arraylistay_ Unshift the function of adding elements at the beginning, the code is as follows:
Copy CodeThe code is as follows: <?php
$array _qlist = Arraylistay (' A ', ' B ');
$array _qlist = Array_merge (Arraylistay (' front ' = ' Hello www.jb51.net '), $array _qlist);
/*
Arraylistay
(
[Front] = Hello www.jb51.net
[0] = a
[1] = b
)
*/
?>
2. + operator, the code is as follows:
Copy CodeThe code is as follows: <?php
$array _qlist = Array (' A ', ' B ');
$array _qlist = Array (' Front ' = ' Hello little www.jb51.net ') + $array _qlist;
?>
The output is the same as using the Arraylistay_merge method.

3. Add an associative array element at the end of the element with the following code:
Copy the Code code as follows: <?php
$array _qlist = Arraylistay (' A ', ' B ');
$array _qlist[' front '] = ' Hello www.jb51.net ';
/*
Output
Arraylistay
(
[0] = a
[1] = b
[Front] = Hello www.jb51.net
)
*/
?>
As for some friends said that the array_push increase element and direct $a[]= ' a ' this performance is almost the same as other, if only one value is not comparable.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/930489.html www.bkjia.com true http://www.bkjia.com/PHPjc/930489.html techarticle PHP array add element method Summary, PHP array element Summary This article summarizes the method of adding elements to the PHP array in detail. Share to everyone for your reference. The specific analysis is as follows: if I ...

  • 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.