How to delete array elements in php and rearrange key values

青灯夜游
Release: 2023-03-12 12:36:01
Original
2237 people have browsed it

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);
?>
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template