$v){...}"."/> $v){...}".">

Home  >  Article  >  Backend Development  >  How to delete array in php foreach

How to delete array in php foreach

藏色散人
藏色散人Original
2021-07-15 09:31:581646browse

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
        )

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

Implementation method:

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

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

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!

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