Home > Backend Development > PHP Problem > How to delete a value in an array in php

How to delete a value in an array in php

王林
Release: 2023-03-03 18:38:01
Original
5143 people have browsed it

How to delete a value in an array in php: You can use the php built-in function array_splice() to delete. This function can remove the selected element from the array, replace it with the new element, and return the array of the removed element, such as [array_splice($array, 1, 1)].

How to delete a value in an array in php

array_splice() function removes the selected element from an array and replaces it with a new element. The function will also return an array of removed elements.

(Recommended tutorial: php graphic tutorial)

Syntax:

array_splice(array,start,length,array)
Copy after login

Parameters:

  • array Required. Specifies array

  • start Required. numerical value. Specifies the starting position of deleted elements

  • length Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array

  • array Optional. Specifies an array with elements to be inserted into the original array

(Video tutorial recommendation: php video tutorial)

Code implementation:

<?php
$array = array(0 => "a", 1 => "b", 2 => "c");
array_splice($array, 1, 1);
                   //↑ Offset which you want to delete
print_r($array);
?>
Copy after login

Output result:

Array(
    [0] => a
    [1] => c
    )
Copy after login

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