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

怎么不用循环删除

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:41:13
Original
1053 people have browsed it

<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);
Copy after login
Copy after login

回复内容:

<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);
Copy after login
Copy after login

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

本质还是循环的

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template