Home  >  Article  >  Backend Development  >  How to remove specified elements from an array in php

How to remove specified elements from an array in php

王林
王林Original
2020-09-03 14:34:292179browse

In PHP, you can use the foreach statement and unset function to remove specified elements from the array. The specific code is [foreach($array as $k=>$v){if($v=='day '){unset($array[$k])}}].

How to remove specified elements from an array in php

We can use the foreach and unset() functions to delete specific elements in the array.

(Related recommendation: php training)

unset() function is used to destroy the given variable.

Grammar:

void unset ( mixed $var [, mixed $... ] )

Example:

 // 销毁单个数组元素
 unset ($bar['quux']);

(Recommended tutorial: php video tutorial)

Code implementation:

foreach($array as $k=>$v){
    if($v == 'day'){
        unset($array[$k]):
    }
}

The above is the detailed content of How to remove specified elements from an 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