Home  >  Article  >  Backend Development  >  How to delete array elements in php and rearrange key values

How to delete array elements in php and rearrange key values

青灯夜游
青灯夜游Original
2021-09-10 18:19:272179browse

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.

How to delete array elements in php and rearrange key values

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:

How to delete array elements in php and rearrange key values

#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:

How to delete array elements in php and rearrange key values

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn