PHP array add element, delete element

Source: Internet
Author: User

Splitting a fraction group

How to add an element to the PHP array: push (), arr[],

PHP code
    1. $arr = Array ();
    2. Array_push ($arr, El1, El2 ... ELN);

But in fact there is a more direct and convenient approach:

PHP code
    1. $arr = Array ();
    2. $arr [] = EL1;
    3. $arr [] = El2;
    4. ...
    5. $arr [] = ELN;

And experiments have proved that the second method is more efficient than the first method is nearly one times higher!

Let's take a look at the following example:

PHP code
    1. $t = Microtime (true);
    2. $array = Array ();
    3. for ($i = 0; $i < 10000; $i + +) {
    4. $array [] = $i;
    5. }
    6. Print Microtime (true)-$t;
    7. print ' <br> ';
    8. $t = Microtime (true);
    9. $array = Array ();
    10. for ($i = 0; $i < 10000; $i + +) {
    11. Array_push ($array, $i);
    12. }
    13. Print Microtime (true)-$t;

Run the script with the result:

Wrote Run 1
0.0054171085357666//Array_push
0.0028800964355469//array[]
Run 2
0.0054559707641602//Array_push
0.002892017364502//array[]
Run 3
0.0055501461029053//Array_push
0.0028610229492188//array[]

Other methods:

1. Add one or more elements at the end of the array.
Array_push () treats the array as a stack and presses the incoming variable into the end of the array. The length of the array will increase depending on the number of variables that are in the stack.
PHP code example:
<?php
$arr 1 = Array ("A", "B");
Array_push ($arr 1, "C", "D");
Print_r ($arr 1);
?>
Operation Result:
Array
(
[0] = a
[1] = b
[2] = C
[3] = d
)
Note: If the first parameter is not an array, Array_push () will issue a warning.
2. Delete an element at the end of the array.
Array_pop () pops up and returns the last cell of the array, minus one of the array's length. If array is empty (or not an array), NULL is returned.
PHP code example:
<?php
$arr 1 = Array ("A", "B", "C", "D");
Array_pop ($arr 1);
Print_r ($arr 1);
?>
Operation Result:
Array
(
[0] = a
[1] = b
[2] = C
)
3. Add one or more elements at the beginning of the array.
Array_unshift () Inserts the incoming cell at the beginning of the array. Note that the units are inserted as a whole, so incoming cells will remain in the same order. All numeric key names are modified to count back from zero, and all text key names remain unchanged.
PHP code example:
<?php
$arr 1 = Array ("C", "D");
Array_unshift ($arr 1, "A", "B");
Print_r ($arr 1);
?>
Operation Result:
Array
(
[0] = a
[1] = b
[2] = C
[3] = d
)
4. Delete the first element of the array and return the first element.
Array_shift () Moves the first cell of the array out and returns as a result, reducing the length of the array and moving all other cells forward one bit. All numeric key names are changed from zero to start, and the text key name is unchanged. If the array is empty (or not an array), NULL is returned.
PHP code example:
<?php
$arr 1 = Array ("A", "B", "C", "D");
Echo (Array_shift ($arr 1));
Print_r ($arr 1);
?>
Operation Result:
A
Array
(
[0] = b
[1] = C
[2] = d
)

Adding elements to the PHP array, deleting elements

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.