Reputation: 1
I've got an array with almost 50K+ sub_arrays. Put all of that in order by priority wasn't the problem, so I've managed to make it look like this:
$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
What I really need is to alternate those sub_arrays, so they can look like this:
$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'
Does anyone know how to do that?
Upvotes: 0
Views: 77
Reputation: 95131
You can try:
//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);
Upvotes: 2