0

I have a long array,for the pagination! I did a "premium" for the users. Now I want to sort array by the premium. My array:

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 [▶]
]

So,that articles have premium_g2 value! The important thing I not want to sort by MYSQL!

I have on every page 8 article. I want to mix the articles,like on every page every second/third articles to be the premium.

Thanks!

1 Answer 1

1

You can use laravel collections.

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

You can also reverse the results.

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

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

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

To mix the articles I think the best solution might be to first group the data into two collections.

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

This will give you a collection with two keys that you can access like so:

$sorted->premium
$sorted->standard

Then you can use the $total count to loop over and select which article to display. You can use the shift() method to pull out the first item from each collection so on the next loop it will select the next item.

@for ($i = 0; $i < $total; $i++)
    @if ($i % 3 === 0)
        {{ $sorted->premium->shift()['make'] }}
    @else
        {{ $sorted->standard->shift()['make'] }}
    @endif
@endfor
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. It was helpful . I forgot to check the Laravel docs.. But to mix? :)
I've added a solution, sorry it took so long but I was trying to find a nice way of doing it and I'm at work. Let me know if there are any problems, I haven't tested but I think this should work.
Only one question. How can I stay on a same ->shift()['make'] ['model']['etc'] . Beacuse if I use always shift ,its moving for the next. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.