用独特的块重复排列数组(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',...],...]

我对algorythmics和高级排序没有太多经验,我觉得有点不知所措。 如果你可以指出我正确的方向。

I want elements of that:

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

to be grouped into chunks with unique values, to make it look like that:

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

First "chunk" '1','2','3','4' contain all unique values without duplicates, second '1','2','3' etc.


The biggest problem is that my array is not combined from simple numbers, it is two-dimensional associative array, something like that:

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

I don't have a lot of experience with algorythmics and advanced sorting and I feel a bit overwhelmed. Please if you could point me out in right direction with that.

最满意答案

试试这个代码:

$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);

Try this code :

$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);

更多推荐