KriiV
KriiV

Reputation: 2020

Split Eloquent result set into two equally-sized chunks

I have an array full of DB records. It could heaps of elements or very little, it changes regularly.

I need to split the array into two equal parts. The reason is I am then passing these arrays to a Laravel view and displaying them into separate columns.

Here is the DB records being pulled:

$books = Book::where('unitcode', '=', $unitcode['unitcode'])->get();

If the pull works then I'm running this:

return View::make('showbooks')
->with('books', $books);

What I want to do is pass $books1 and $books2 which is actually $books split into 2 parts.

Upvotes: 4

Views: 17398

Answers (6)

Milad Parirokh
Milad Parirokh

Reputation: 81

splitIn()

The splitIn method breaks a collection into the given number of groups, filling non-terminal groups completely before allocating the remainder to the final group:

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
 
$groups = $collection->splitIn(3);
 
$groups->all();

// [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]

Upvotes: 0

Hans Zhang
Hans Zhang

Reputation: 509

Use:

$collection->chunk(2);

See: https://laravel.com/docs/5.6/collections#method-chunk

Upvotes: 0

Jerre
Jerre

Reputation: 179

To expand on Ohgodwhy's answer:

Since you're using , here's a more fluent way to achieve this:

$books = collect($books);

$halved = $books->split(ceil($books->count()/2))->toArray();

// Result: [[a, b, c], [d, e, f]]

Take a look at Laravel's various Collection Methods for more available methods to fluently manipulate arrays.

Upvotes: 0

Abdessabour Mtk
Abdessabour Mtk

Reputation: 3888

I think you should take the length of the array using the count method and then loop the items till the middle it should look like this :

<?php 
    $count = count($books);            //getting the count of $books
    $mid = round($count/2);           //getting the mid point of $books
    for($i = 0; $i < $count ; $i++){ //looping through the indexes
        if($i <= mid - 1){          //Checking the position of $i and adding the value
            $books1[] = $books[$i];
        }else{
            $books2[] = $books[$i];
        }
    }
?>

Upvotes: 0

Mad Angle
Mad Angle

Reputation: 2330

I think this will work for you.

<?php
    $books = array("a", "b", "c", "d", "e","f"); // assuming books is an array like this
    $count = count($books); // total count of the array books
    $half = $count/2; // half of the total count
    $books1 = array_slice($books, 0, $half);      // returns first half
    $books2 = array_slice($books, $half);  // returns second half

    print_r($books1);
    print_r($books2);
?>

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50787

This is a one liner:

$halved = array_chunk($books, ceil(count($books)/2));

Then $halved[0] will contain the first half of the array. It will always be 1 element larger in the event that the array contains an odd number of elements. Of course, $halved[1] will contain the 2nd half of the array.

Here's a working example

Upvotes: 24

Related Questions