数组排序,不是简单的排序

时间:2017-01-23 10:04:46

标签: php mysql arrays laravel sorting

我有一个很长的阵列,为了分页! 我为用户做了“溢价”。现在我想按溢价排序数组。 我的阵列:

array:8 [▼
  0 => array:34 [▼
    "id" => 10
    "id_user" => "2"
    "vehicletype" => "car"
    "make" => "AMC"
    "model" => "Encore"
    "age" => "2017-01-14"
    "body" => "4"
    "price" => "94650"
    "vrt_price" => "102850"
    "vrt_date" => "2017-01-14"
    "condition" => "2"
    "mileage" => "12"
    "booth_space" => "1"
    "doors" => "1"
    "seat" => "14"
    "eng_size" => "1"
    "eng_hp" => "0.75"
    "eng_kw" => "1"
    "fuel" => "LPG"
    "trans" => "Semi-Au"
    "desc" => "133333333333333333333333333333333333333333333333333333333133333333333333333333333333333333333333333333333333333333"
    "extras" => "2"
    "created_at" => "2017-01-14 14:13:00"
    "finish_at" => "2017-02-14 14:13:00"
    "updated_at" => null
    "premium_g1" => null
    "g1_start_at" => null
    "g1_finish_at" => null
    "premium_g2" => null
    "g2_start_at" => null
    "g2_finish_at" => null
    "extras_title" => array:1 [▶]
    "user_name" => "Szabolcs996"
    "images" => array:1 [▶]
  ]
  1 => array:34 [▶]
  2 => array:34 [▶]
  3 => array:34 [▶]
  4 => array:34 [▶]
  5 => array:34 [▶]
  6 => array:34 [▼
    "id" => 5
    "id_user" => "2"
    "vehicletype" => "car"
    "make" => "Aston Martin"
    "model" => "Rapide"
    "age" => "2017-01-14"
    "body" => "5"
    "price" => "4800"
    "vrt_price" => "1550"
    "vrt_date" => "2017-01-14"
    "condition" => "2"
    "mileage" => "450"
    "booth_space" => "1550"
    "doors" => "15"
    "seat" => "4"
    "eng_size" => "5"
    "eng_hp" => "79.5"
    "eng_kw" => "106"
    "fuel" => "LPG"
    "trans" => "Automatic"
    "desc" => "1234567891111111111111111111111111111111111111111111111111111115asssssssssssssssssssssss"
    "extras" => "2,4,7,8"
    "created_at" => "2017-01-14 12:43:19"
    "finish_at" => "2017-02-14 12:43:19"
    "updated_at" => "2017-01-22 17:04:08"
    "premium_g1" => "123"
    "g1_start_at" => "2017-01-23 09:45:58"
    "g1_finish_at" => "2017-02-04 09:45:58"
    "premium_g2" => "22"
    "g2_start_at" => "2017-01-23 09:45:58"
    "g2_finish_at" => "2017-02-22 09:45:58"
    "extras_title" => array:4 [▶]
    "user_name" => "Szabolcs996"
    "images" => array:12 [▶]
  ]
  7 => array:34 [▶]
]

所以,这些文章有premium_g2价值! 重要的是我不想按MYSQL排序!

我每页都有8篇文章。 我想把文章混合起来,就像每一页每隔一秒/第三篇文章一样。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用laravel集合。

collect($array)->sortBy('premium_g2');

您也可以撤消结果。

collect($array)->sortByDesc('premium_g2');

collect($array)->sort('premium_g2')->reverse();

https://laravel.com/docs/5.3/collections#method-sortby

要混合文章,我认为最好的解决方案可能是首先将数据分组为两个集合。

$collection = collect($array);
$total = $collection->count();
$sorted = $collection->groupBy(function ($item, $key) {
    return $item['premium_g2'] === null ? 'standard' : 'premium';
}

这将为您提供一个包含两个键的集合,您可以这样访问:

$sorted->premium
$sorted->standard

然后,您可以使用$total计数循环并选择要显示的文章。您可以使用shift()方法从每个集合中提取第一个项目,这样在下一个循环中它将选择下一个项目。

@for ($i = 0; $i < $total; $i++)
    @if ($i % 3 === 0)
        {{ $sorted->premium->shift()['make'] }}
    @else
        {{ $sorted->standard->shift()['make'] }}
    @endif
@endfor