按唯一块对具有重复项的数组进行排序


Sort array with duplicates by unique chunks

我想要这样的元素:

['1','1','2','2','2','3','3','3','3','3','3','3','4']

将其分组为具有唯一值的块,使其看起来像:

['1','2','3','4','1','2','3','2','3','3','3','3','3']

第一个"区块"'1','2','3','4'包含所有不重复的唯一值,第二个'1','2','3'


最大的问题是,我的数组不是由简单的数字组合而成的,它是二维关联数组,类似于

[['id'=>'xd1c',...],['id'=>'ab2c',...],['id'=>'xd1c',...],['id'=>'xd1c',...],['id'=>'ab2c',...],['id'=>'xd1c',...],['id'=>'687d',...],...]

我对算法和高级排序没有太多经验,我觉得有点不知所措。请你们给我指出正确的方向。

尝试以下代码:

$current = array('1','1','2','2','2','3','3','3','3','3','3','3','4');
$new = array();
while(!empty($current)){
    foreach(array_keys(array_unique($current)) as $index){
        $new[] = $current[$index];
        unset($current[$index]);
    }
}
print_r($new);