Home  >  Article  >  Backend Development  >  怎么不用循环删除

怎么不用循环删除

WBOY
WBOYOriginal
2016-06-06 20:41:131018browse

<code>$arr = [
    ['id'=>1,'name'=>'php'],
    ['id'=>2,'name'=>'js'],
    ['id'=>3,'name'=>'python'],
    ['id'=>4,'name'=>'java']
];
//删除id为4的一维数组,除了unset还有其他方法吗
foreach($arr as $key=>$value){
    if($value['id'] == 4) unset($arr[$key]);
}
echo '<pre class="brush:php;toolbar:false">';print_r($arr);

回复内容:

<code>$arr = [
    ['id'=>1,'name'=>'php'],
    ['id'=>2,'name'=>'js'],
    ['id'=>3,'name'=>'python'],
    ['id'=>4,'name'=>'java']
];
//删除id为4的一维数组,除了unset还有其他方法吗
foreach($arr as $key=>$value){
    if($value['id'] == 4) unset($arr[$key]);
}
echo '<pre class="brush:php;toolbar:false">';print_r($arr);

<code>print_r(array_filter($arr, function($var) {return $var['id']!=4;}));
</code>

本质还是循环的

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