Method: 1. Use the array_splice() function to delete the specified number of elements starting from the specified position in the array. The syntax is "array_splice($arr, position, format)"; 2. After deleting the array elements, use sort( ) or rsort() function sorts array elements in ascending or descending order.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can delete array elements Use the array_splice() function. The array_splice() function removes a selected element from an array and replaces it with a new element. The function will also return an array of removed elements.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "删除后的数组:" ; array_splice($arr,1,2); var_dump($arr); ?>
Output result:
#After deleting the array elements, you can use the sort() or rsort() function to sort the array elements in ascending or descending order Sort.
echo "升序排序:" ; sort($arr); var_dump($arr); echo "降序排序:" ; rsort($arr); var_dump($arr);
Output results:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to delete array elements in php and rearrange key values. For more information, please follow other related articles on the PHP Chinese website!