How to delete array in php foreach

藏色散人
Release: 2023-03-11 14:52:01
Original
1655 people have browsed it

php foreach method to delete an array: first create a PHP sample file; then define an array; finally delete through "foreach($db as $k=>$v){...}" That’s it.

How to delete array in php foreach

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

Specific questions:

How to delete an array in php foreach?

PHP foreach delete array

Array output, I want to delete one value and display the remaining values.

Array:

Array
(
    [0] => Array
        (
            [uid] => 1
            [tid] => 0
            [username] => 123
            [truename] => 3333
            [groupid] => 1
        )
    [1] => Array
        (
            [uid] => 2
            [tid] => 0
            [username] => 321
            [truename] => 5
            [groupid] => 1
        )
    [2] => Array
        (
            [uid] => 4
            [tid] => 0
            [username] => 456
            [truename] => 5
            [groupid] => 2
        )
Copy after login

I only want to display the array of [groupid] => 2, and delete [groupid] => 1.

foreach($db as $v){  
    if($v['groupid'] == 1){
        unset($db[$v['groupid']]);
    }
}
Copy after login

Implementation method:

foreach($db as $k=>$v){  

    if($v['groupid'] == 1){        unset($db[$k]);
    }
}
Copy after login

Recommended learning: "PHP Video Tutorial"

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