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.
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!