Home Daily Programming PHP Knowledge How to delete PHP array element key value and reorder

How to delete PHP array element key value and reorder

Jan 16, 2019 pm 02:06 PM

I want to delete the key value of an element in the PHP array and then re-standardize the index sorting. We can use the built-in functions unset() and array_values() in PHP to achieve this.

How to delete PHP array element key value and reorder

# Below we will combine simple code examples to introduce to you how to delete the key values ​​​​of PHP array elements and reorder them.

The code example is as follows:

<?php

//删除数组元素键值重新排序

$x = array(1,2,3,4,5);
var_dump($x);

unset($x[3]);
$x = array_values($x);

var_dump($x);
Copy after login

In this code, we print the array $x before deleting the $x[3] element, after deletion without reordering, and after deletion. Sorted results.

The sorting comparison is shown in the figure below:

How to delete PHP array element key value and reorder

As shown in the figure, we use array_values() to re-index the array after deleting elements.

Related function introduction:

unset: Release the given variable

unset ( mixed $var [, mixed $... ] ) : void
Copy after login

unset() Destroy the specified Variables. The behavior within the function will vary depending on the type of variable you want to destroy.

If you unset() a global variable in a function, only the local variable will be destroyed, and the variables in the calling environment will maintain the same value before calling unset().

array_values: Returns all the values ​​in the array

array_values ( array $array ) : array
Copy after login

array_values() Returns all the values ​​in the input array and creates a numerical index for them.

Parameter array array. Return value, returns the index array containing all values.

This article is an introduction to the method of deleting and reordering the key values ​​of PHP array elements. It is simple and easy to understand. It is also one of the common PHP interview questions. I hope it will be helpful to friends in need!


The above is the detailed content of How to delete PHP array element key value and reorder. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)