There are many ways to delete array elements and add array elements in php. the following section will summarize some practical and common methods to add and delete instances for you. Add data element functions
There are many ways to delete array elements and add array elements in php. the following section will summarize some practical and common methods to add and delete instances for you.
Added the data element functions, including array_push () and array_unshift ().
1. add elements at the end of the array
1. array_push:
- $ Stack = array ("orange", "banana ");
- Array_push ($ stack, "apple", "raspberry ");
- Print_r ($ stack );
- ?>
Output:
- Array
- (
- [0] => orange
- [1] => banana
- [2] => apple
- [3] => raspberry
- )
2. $ arr:
- $ Arr = array ("orange", "banana ");
- $ Arr [] = 'apple ';
- Print_r ($ arr );
- ?>
The two methods have the same effect,Note:If array_push () is used to add a unit to the array, it is better to use $ array [] =, because there is no additional burden to call the function.
2. insert elements at the beginning of the array
1. the array_unshift code is as follows:
- $ Queue = array ("orange", "banana ");
- Array_unshift ($ queue, "apple", "raspberry ");
- Print_r ($ queue );
- ?>
Output
- Array
- (
- [0] => apple
- [1] => raspberry
- [2] => orange
- [3] => banana
- )
Delete the array element unset or directly set null. if you want to delete an element in an array, you can use the unset directly, but what you see today surprised me. the code is as follows:
- $ Arr = array ('A', 'B', 'C', 'D ');
- Unset ($ arr [1]);
- Print_r ($ arr );
- ?>
- Print_r ($ arr)
The result is not that. The final result is Array ([0] => a [2] => c [3] => d ), so how can we fill in the missing elements and re-index the array? The answer is array_splice (). The code is as follows:
- $ Arr = array ('A', 'B', 'C', 'D ');
- Array_splice ($ arr, 1, 1 );
- Print_r ($ arr );
- ?>
After print_r ($ arr), the result is Array ([0] => a [1] => c [2] => d)
Deletes the specified element of an array.
For example, the array_slice () function extracts a value from the array based on the conditions and returns the result.
Array_slice (array, offset, length, preserve)
Array: array
Offset: specifies the start position of the retrieved element. if it is a positive number, it is obtained from the beginning to the end. if it is a negative value, it is taken from the back to the end to the absolute value of the offset. the code is as follows:
- $ A = array (0 => "Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird ");
- Print_r (array_slice ($ a, 1, 2 ));
- ?>
- Output
- Array ([0] => Cat [1] => Horse)
The array_shift () function also deletes the first element in the array and returns the value of the deleted element. the relative array_pop () function deletes the last element in the array, when several functions are used, the array_search () function is more practical. the array_search () function is the same as the in_array () function. find a key value in the array. if this value is found, the key name of the matching element is returned. If no key is found, false is returned. the code is as follows:
- $ Array = array ('1', '2', '3', '4', '5 ');
- $ Del_value = 3;
- Unset ($ array [array_search ($ del_value, $ array)]); // use unset to delete this element
- Print_r ($ array );
- Output
- Array ('1', '2', '4', '5 ');