Splitting a fraction group
How to add an element to the PHP array: push (), arr[],
PHP code
- $arr = Array ();
- Array_push ($arr, El1, El2 ... ELN);
But in fact there is a more direct and convenient approach:
PHP code
- $arr = Array ();
- $arr [] = EL1;
- $arr [] = El2;
- ...
- $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
- $t = Microtime (true);
- $array = Array ();
- for ($i = 0; $i < 10000; $i + +) {
- $array [] = $i;
- }
- Print Microtime (true)-$t;
- print ' <br> ';
- $t = Microtime (true);
- $array = Array ();
- for ($i = 0; $i < 10000; $i + +) {
- Array_push ($array, $i);
- }
- 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