Home >Backend Development >PHP Problem >How to delete a field in a two-dimensional array in php

How to delete a field in a two-dimensional array in php

藏色散人
藏色散人Original
2021-06-16 10:44:044295browse

How to delete a certain field in a two-dimensional array in php: first create a PHP sample file; then use the array_walk and array_diff functions to delete a certain field in the array.

How to delete a field in a two-dimensional array in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php How to delete a field in a two-dimensional array?

The two functions array_walk and array_diff are mainly used here.

Example:

$list = [
    0 => [
        'id' => 1001,
        'name' => '张三',
        'sex' => '男'
    ],
    1 => [
        'id' => 2091,
        'name' => '李四',
        'sex' => '女'
    ]
];

Assuming that you need to remove the name sex column in the above array, you can directly:

array_walk($list, function (&$item) {
    $item = array_diff($item, ['name', 'sex']);
});

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to delete a field in a two-dimensional 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