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

藏色散人
Release: 2023-03-10 16:22:01
Original
4246 people have browsed it

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' => '女'
    ]
];
Copy after login

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']);
});
Copy after login

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!

Related labels:
php
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