Laravel对集合进行排序(分块)

时间:2019-03-15 17:20:27

标签: php laravel-5

最有可能是一个简单的问题,但我的头有些发麻。任何帮助将不胜感激。

我想将来自驱动程序ID的结果与路线ID合并,例如JSON结果。

"6a": [
 [
    1,
    2,
    3,
    4,
    5
  ],
 "9a": [
  [
    6,
    7,
    8,
    9,
    10
   ]

public function triggerMerge()
    {

        $RouteID = Route::all()->pluck('route_id');
        $DriverID = Driver::all()->pluck('driver_id')->chunk(5);

        foreach ($RouteID as $key => $route) {
            foreach ($DriverID as $k => $driver) {

                $variables[$route][] = $driver;

            }

        }
    }

以上函数仅将块组合到数组的第二个索引。我想按数字顺序将每个块分离到数组第一部分的每个索引。

当前结果是

variables": {
 "6a": [
         [
          1,
          2,
          3,
          4,
          5
         ],
          {
         "5": 6,
         "6": 7,
         "7": 8,
         "8": 9,
         "9": 10
          },
         {
         "10": 11,
         "11": 12,
         "12": 13,
         "13": 14,
         "14": 15
         },
         {
         "15": 16,
         "16": 17,
         "17": 18,
         "18": 19,
          "19": 20
         },
        {
         "20": 21,
         "21": 22,
         "22": 23,
         "23": 24,
         "24": 25
         },
         {
         "25": 26,
         "26": 27,
         "27": 28,
         "28": 29,
         "29": 30
          }
          ],
 "9a": [
         [
         1,
         2,
         3,
         4,
         5
          ],
         {
        "5": 6,
        "6": 7,
        "7": 8,
        "8": 9,
        "9": 10
        },
        {
       "10": 11,
       "11": 12,
       "12": 13,
       "13": 14,

       "14": 15
       },
       {
       "15": 16,
       "16": 17,
       "17": 18,
       "18": 19,
       "19": 20
        },
        {
       "20": 21,
       "21": 22,
       "22": 23,
       "23": 24,
       "24": 25
      },
       {
       "25": 26,
       "26": 27,
      "27": 28,
       "28": 29,
      "29": 30


}
],

对此的任何帮助都将很棒

1 个答案:

答案 0 :(得分:1)

也许是这样的:

$results = [];

Driver::all()->pluck('driver_id', 'route_id')->chunk(5)->each(function($driveId) use ($RouteID, &$results) {
    $results['route_id'][] = $driveId;
});