我正在寻找一个更好的方法来解决我的问题。
我的数组看起来像这样,并按releaseDate:
排序array:12 [
0 => array:3 [
"id" => "11602"
"released" => false
"releaseDate" => "2015-09-09"
],
1 => array:3 [
"id" => "11601"
"released" => false
"releaseDate" => "2015-08-25"
],
2 => array:3 [
"id" => "11600"
"released" => true
"releaseDate" => "2015-08-25"
],
...
]
我想只保留最接近发布的行("released" => false),并删除其他的。
我当前的解决方案是:
private function keepOneVersionUnreleased(array $array)
{
for ($i = 0; $i < count($array); $i++) {
if ((isset($array[$i+1])) && ($array[$i]['released'] === false) && ($array[$i+1]['released'] === false)) {
unset($array[$i]);
}
}
return $array;
}
谁有更好的解决方案?
谢谢
您可以简单地使用array_filter
作为
$result = array_filter($your_array,function($v){ return ($v['released'] === false);});
演示这个怎么样:
foreach ($array as $key => $value)
{
if ((isset($array[$key+1)) && ($array[$key]['released'] === false && ($array[$key+1]['released'] === false)){
unset($array[$key]);
}
}
return $array;