In php, The array_push function is commonly used to add elements to an array, that is, the array's inbound stack operation. However, this function adds elements to the end of the array, if you want to press an array element into the array header (that is, the first element), how can this problem be solved?
Use the php built-in function array_unshift. The following describes how to use array_unshift.
Array_unshift
This function inserts one or more elements at the beginning of an array. The added elements are added as a whole. The order of these elements in the array is the same as that in the parameter. The names of all numeric keys in the modified array are changed to count again from scratch, and the names of all text keys remain unchanged. This function returns the number of elements in the array after the operation.
Syntax: int array_unshift (array & $ array, mixed $ var [, mixed $...])
For example:
<?php$queue = array("phpernote",".com");array_unshift($queue,"apple","raspberry");print_r($queue);
The above routine will output:
Array
(
[0] => apple
[1] => raspberry
[2] => phpernote
[3] =>. com
)
Articles you may be interested in
- Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())
- Remove duplicate values and array_unique functions from two-dimensional arrays
- PHP merges arrays + and array_merge
- PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function
- Php obtains all the files in the directory and saves the results to the array program.
- PHP rounding precision decimal places and Rounding Methods
- How does php Delete the first and last elements of an array?
- How to insert an element to the front of the body element in js