我想在子数组中将相同的值分组,同时保留每组值的原始顺序。
我想要这个:
array('a','b','b','c','c','c','a','a');
成为:
array( array('a'),array('b','b'),array('c','c','c'),array('a','a'));
答案 0 :(得分:0)
$source = array('a','b','b','c','c','c','a','a');
$tempvalue = false;
$temparr = array();
$new = array();
foreach ($source as $value) {
echo $value;
if ($tempvalue && $value != $tempvalue){
$new[] = $temparr;
$temparr = array();
}
$temparr[] = $value;
$tempvalue = $value;
}
$new[] = $temparr;
echo json_encode($new);
输出:
[["a"],["b","b"],["c","c","c"],["a","a"]]