The general routine is simply to populate the $ reducedValues ββarray with the values ββyou want to reduce to single.
$before = array(1,2,2,2,3,3,4,4,4,5,5); $reduceValues = array(3,5); $toReduce = array_fill_keys($reduceValues,TRUE); $after = array_filter( $before, function($data) use ($reduceValues,&$toReduce) { if (in_array($data,$reduceValues)) { if ($toReduce[$data]) { $toReduce[$data] = FALSE; return TRUE; } return FALSE; } return TRUE; } ); var_dump($after);
source share