Nomeqc
1/6/2020 - 1:41 AM

PHP数组根据条件批量删除

<?php
function delete_items_by_ids(&$list, $ids) {
    $arr = array_filter($list, function($val) use($ids){
    	return !in_array(intval($val['id']), $ids);
    });
    $list = array_values($arr);
}

$ids = [1];
$list = [['id'=> 1, 'name'=>'tom'], ['id'=> 2, 'name'=>'mike']];

delete_items_by_ids($list, $ids);

echo json_encode($list);
#输出:[{"id":2,"name":"mike"}]