Home  >  Article  >  Backend Development  >  How to remove a key from an array in php

How to remove a key from an array in php

藏色散人
藏色散人Original
2022-01-04 09:29:333068browse

php method to remove a key from an array: 1. Create a PHP sample file; 2. Determine the key of the array element to be deleted; 3. Delete the specified key through the array_diff_key function.

How to remove a key from an array in php

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

How to remove a key from an array in php?

array_diff_key() method:

If you know the key of the array element you want to delete, you can use array_diff_key(). You need to enter the key to be deleted in the key value position of the second parameter of the function. The value is not required and can be optional.

<?php
$array = array(0 => "a", 1 => "b", 2 => "c");
$array = array_diff_key($array, [0 => "xy", "2" => "xy"]);
                               //↑           ↑ 你要删除的数组键
print_r($array);
?>

The output result is:

Array (
    [1] => b
)

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove a key 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