Home  >  Article  >  Backend Development  >  How to delete specified elements from an array in php

How to delete specified elements from an array in php

藏色散人
藏色散人Original
2018-11-26 11:00:179567browse

php method to delete the specified elements of the array: first create a PHP sample file; then define an array; finally delete the specified elements of the array through the unset function.

How to delete specified elements from an array in php

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

This article mainly introduces to you the specific implementation method of PHP to delete specified elements of an array.

In the process of learning PHP, we usually encounter problems related to arrays. Then deleting array elements is a common question and is also one of the interview test points.

Below we will introduce to you through a simple example:

Delete an element from the array. After deleting the element, the integer key must be standardized.

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

unset($x[3]);
$x = array_values($x);
echo &#39;&#39;;
var_dump($x);

The result is as shown in the figure below:

How to delete specified elements from an array in php

In the above code, we first use the unset function to set the subscript in the array $x to 3 The array element is deleted, and the array value with subscript 3 is 4. Then we use the array_values ​​function to normalize the integer keys of the new array after the element is deleted.

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

array_values() function Returns an array containing all key values ​​​​in the given array, but does not retain the key names.

This article is about PHP's method of deleting specified elements of an array and normalizing integer keys. It is also very simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of How to delete specified elements from an array in php. 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