- $ Fruits = array ("apple", "banana ");
- Array_unshift ($ fruits, "orange", "pear ")
- // $ Fruits = array ("orange", "pear", "apple", "banana ");
Add elements at the end of the array The return value of the array_push () function is int type, which is the number of elements in the array after data is pressed. for this function, multiple variables can be passed as parameters and multiple variables can be pushed to the array at the same time. The format is: (array, mixed variable [, mixed variable...]) The following example adds two more fruits to the $ fruits array:
- $ Fruits = array ("apple", "banana ");
- Array_push ($ fruits, "orange", "pear ")
- // $ Fruits = array ("apple", "banana", "orange", "pear ")
Delete value from array header The array_shift () function deletes and returns the elements found in the array. The result is that if a numeric value is used, all corresponding values are moved down, and the array using the correlated key is not affected. The format is mixed array_shift (array) The following example deletes the first element apple in the $ fruits array:
- $ Fruits = array ("apple", "banana", "orange", "pear ");
- $ Fruit = array_shift ($ fruits );
- // $ Fruits = array ("banana", "orange", "pear ")
- // $ Fruit = "apple ";
Deletes an element from the end of an array. The array_pop () function deletes the array and returns the last element of the array. The format is mixed array_pop (aray target_array ); The following example deletes the last state from the $ states array:
- $ Fruits = array ("apple", "banana", "orange", "pear ");
- $ Fruit = array_pop ($ fruits );
- // $ Fruits = array ("apple", "banana", "orange ");
- // $ Fruit = "pear ";
Note: PHP provides some functions for extending and downgrading arrays. For programmers who want to simulate various queue implementations (FIFO and LIFO), these functions can be convenient. As the name suggests, the function names (push, pop, shift, and unshift) of these functions clearly reflect their functions. A traditional queue is a data structure. Deleting elements in the same order as adding elements is called FIFO or FIFO. On the contrary, the stack is another data structure, in which the order in which elements are deleted is the opposite of the order when they are added, which is either backward-forward, FIFO, or LIFO. |