Creating an array stops saying the array () function
The array can be extracted using the list ().
To test an array element, you can use Is_array ()
Takes a parameter, passes in the variable, if the variable is an array, returns true if it is not returned false.
Adding and removing array elements
FIFO and LIFO of arrays
Let's explain what FIFO and LIFO are.
The deletion element is the same as the order in which elements are added, known as FIFO, both first-in-first-out
The deletion of elements is in contrast to the order in which elements are added, called LIFO, both Last-in-first-out
Operation of the corresponding array
- Array_unshift ()
- int Aray_unshift (array array,mixed variable [, mixed variable ...])
Adding elements to the array header, all existing numeric keys are changed to reflect the new position in the array, and the association key is unaffected.
Cases:
<?php
$words = Array (' A ', ' B ', ' C ');
Print_r ($words);
Array ([0]->a,[1]->b,[2]->c);
Array_unshift ($words, ' d ');
Print_r ($words);
Array ([0]->d,[1]->a,[2]->b,[3]->c);
- Array_shift ()
- Mixed Array_shift (array array)
Deletes an element from the array header, deletes and returns the element found in the array, and the result is that if the value key is used, all corresponding values are moved down. If you are using an association key, the array is not affected.
Cases:
<?php
$words = Array (' A ', ' B ', ' C ');
Print_r ($words);
Array ([0]->a,[1]->b,[2]->c);
Array_shift ($words, ' a ');
Print_r ($words);
Array ([0]->b,[1]->c);
- Array_push ()
- int Array_push (array array, mixed variable [, mixed variable ...])
Adds an element from the end of an array, adds variable to the end of the array, and returns false for success. You can pass multiple arguments as input and press multiple variables into the array at the same time.
Cases:
<?php
$stack = Array ("Orange", "banana");
Array_push ($stack, "apple", "raspberry");
Print_r ($stack);
?>
This example causes $stack to have the following units:
Array ([0] => Orange [1] => banana [2] => Apple [3] => Raspberry)
-------------------------------------------------------------
For array operations, using $array[]= can increase efficiency. Because you don't need to call a function.
-------------------------------------------------
- Array_pop ()
- Mixed Array_pop (array target_array)
Deletes an element from the end of the array and returns the last element of the array.
<?php
$stack = Array ("Orange", "banana", "apple", "raspberry");
$fruit = Array_pop ($stack);
Print_r ($stack);
?>
After this operation, the $stack will have only 3 units:
Array ([0] => Orange [1] => banana [2] => Apple)
Own some summary and point of view.
The above four functions, sometimes can be very useful, first of all say return value
Note that the return value of Array_unshift () and Array_push () should be boolean, but the manual and related books are labeled Int. This is worth thinking about.
The Array_shift () and Array_pop () two functions can not only implement elements that delete the related array, but also return the elements of the array that were deleted. Write here first, there are a lot of back.