Home > Article > Backend Development > How to add and delete array elements in php
Add elements at the end of the array The return value of thearray_push() function is of type int, which is the number of elements in the array after pushing the data. You can pass multiple variables as parameters to this function and push multiple variables into the array at the same time. Its form is: (array array,mixed variable [,mixed variable...]) The following example adds two more fruits to the $fruits array:
Delete value from array head array_shift() function removes and returns the element found in the array. The result is that if you are using numeric keys, all corresponding values are shifted down, while arrays using associative keys are not affected. Its form is: mixed array_shift(array array) The following example deletes the first element apple in the $fruits array:
Delete elements from the end of the array array_pop() function removes and returns the last element of the array. Its form is: mixed array_pop(aray target_array); The following example removes the last state from the $states array:
Remarks: PHP provides some functions for expanding and shrinking arrays. These functions provide convenience for programmers who wish to emulate various queue implementations (FIFO, LIFO). As the name suggests, the function names of these functions (push, pop, shift, and unshift) clearly reflect their functions. The traditional queue is a data structure. The order of deleting elements and adding elements is the same, which is called first-in-first-out, or FIFO. In contrast, a stack is another data structure in which elements are removed in the reverse order in which they were added. This becomes last-in-first-out, or LIFO. |