情景:
传入脚本的数据:
// Sample data, numbers might not be in this order or in ascending order,
// they are IDs of items
$items = array(
1, 2, 3, 4, 5, 6, 7, 9, 10
);
// Selected users (IDs)
$users = array(
551,
552,
553,
554
);
$partitions = array_chunk($items, count($users));
print_r($partitions);
提供以下输出:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
[2] => Array
(
[0] => 9
[1] => 10
)
)
所需的输出是将该数组拆分为4个较小的数组,其中包含偶数个项目,因此所需的输出将是这样的:
Array
(
[0] => Array
(
[0] => 1
[1] => 5
[2] => 9
)
[1] => Array
(
[0] => 2
[1] => 6
[2] => 10
)
[2] => Array
(
[0] => 3
[1] => 7
)
[3] => Array
(
[0] => 4
[1] => 8
)
)
是否有PHP功能我可以这样做或者我必须自己编写?
编辑:此问题与Split array into a specific number of chuncks不同,因为我需要输出以获得商品ID的顺序。另一个问题并没有接近那个输出。