2013-04-29 78 views
0

我有一个几乎有50K +子数组的数组。把所有的,为了优先不是问题,所以我设法使它看起来像这样:如何创建按交替优先级排序的数组?

$SendQueue = array(
    array('3', '+553400000001', 'My text messege here'), 
    array('3', '+553400000002', 'My text messege here'), 
    array('3', '+553400000003', 'My text messege here'), 
    array('3', '+553400000004', 'My text messege here'), 
    array('3', '+553400000005', 'My text messege here'), 
    array('3', '+553400000006', 'My text messege here'), 
    array('3', '+553400000007', 'My text messege here'), 
    array('3', '+553400000008', 'My text messege here'), 
    array('3', '+553400000009', 'My text messege here'), 
    array('3', '+553400000010', 'My text messege here'), 
    array('3', '+553400000011', 'My text messege here'), 
    array('3', '+553400000012', 'My text messege here'), 
    array('2', '+553400000013', 'My text messege here'), 
    array('2', '+553400000014', 'My text messege here'), 
    array('2', '+553400000015', 'My text messege here'), 
    array('2', '+553400000016', 'My text messege here'), 
    array('2', '+553400000017', 'My text messege here'), 
    array('2', '+553400000018', 'My text messege here'), 
    array('1', '+553400000019', 'My text messege here'), 
    array('1', '+553400000020', 'My text messege here'), 
); 
//where '3', '2' and '1' are my priorities' types 

我真正需要的是那些交替sub_arrays,这样他们就可以是这样的:

$SendQueue = array(
    array('3', '+553400000001', 'My text messege here'), 
    array('3', '+553400000002', 'My text messege here'), 
    array('3', '+553400000003', 'My text messege here'), 
    array('3', '+553400000004', 'My text messege here'), 
    array('3', '+553400000005', 'My text messege here'), 
    array('3', '+553400000006', 'My text messege here'), 
    array('2', '+553400000013', 'My text messege here'), 
    array('2', '+553400000014', 'My text messege here'), 
    array('2', '+553400000015', 'My text messege here'), 
    array('1', '+553400000019', 'My text messege here'), 
    array('3', '+553400000007', 'My text messege here'), 
    array('3', '+553400000008', 'My text messege here'), 
    array('3', '+553400000009', 'My text messege here'), 
    array('3', '+553400000010', 'My text messege here'), 
    array('3', '+553400000011', 'My text messege here'), 
    array('3', '+553400000012', 'My text messege here'), 
    array('2', '+553400000016', 'My text messege here'), 
    array('2', '+553400000017', 'My text messege here'), 
    array('2', '+553400000018', 'My text messege here'), 
    array('1', '+553400000020', 'My text messege here'), 
); 
//Each 10 sub_arrays, 6 of them are priority '3', 3 of them are priority '2' and 1 of them is priority '1' 

有谁知道该怎么做?

+1

RTLM:http://php.net/usort – 2013-04-29 17:32:55

+0

之间PHP内置的阵列功能和循环没有太多你不能做阵列。你只需*尝试*。 – 2013-04-29 17:33:20

+0

http://php.net/manual/en/class.splpriorityqueue.php – vascowhite 2013-04-29 17:34:05

回答

2

你可以试试:

//Group the array 
$group = array_reduce($SendQueue, function ($a, $b) { 
    $a[$b[0]][] = $b; 
    return $a; 
}); 


$final = array(); 

while(count($final) < count($SendQueue)) { 
    // 6 of them are priority '3' 
    $final = array_merge($final, array_slice($group[3], 0, 6)); 
    // 3 of them are priority '2' 
    $final = array_merge($final, array_slice($group[2], 0, 3)); 
    // 1 of them are priority '1' 
    $final = array_merge($final, array_slice($group[1], 0, 1)); 
} 

print_r($final); 

See Live Demo

+1

好的&快速解决方案.. +1 – Sumoanand 2013-04-29 18:14:27

+0

谢谢!它在我的应用 – 2013-04-29 19:00:20

+0

完美工作,欢迎您... – Baba 2013-04-29 19:48:28