5
$\begingroup$

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.

$\endgroup$
10
  • $\begingroup$ @scrowler Would you please be able to post an answer on its use? I'm quite new to this and am having trouble finding the length of the array to then chunk it. $\endgroup$ Commented Aug 28, 2014 at 3:45
  • $\begingroup$ $books = array_chunk($books, count($books)/2); - split it in half $\endgroup$ Commented Aug 28, 2014 at 3:46
  • $\begingroup$ @scrowler Thanks! Would getting the other half be as simple as: $books = array_chunk($books, count($books)/2, count($books);? $\endgroup$ Commented Aug 28, 2014 at 3:48
  • $\begingroup$ $books would then contain an array with two keys, each key would contain half of the array. You could also do list($books1, $books2) = array_chunk($books, count($books)/2); to get them into variables $\endgroup$ Commented Aug 28, 2014 at 3:49
  • $\begingroup$ if you use array_chunk and it's a odd number of elements, it will create an array with 3 arrays inside of it, not 2 arrays with +1 element in each array. $\endgroup$ Commented Aug 28, 2014 at 3:50

6 Answers 6

25
$\begingroup$

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.

$\endgroup$
Sign up to request clarification or add additional context in comments.

Comments

1
$\begingroup$

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);
?>
$\endgroup$

1 Comment

If $books has an odd number of elements, one element will be left off... I would remove the 2nd $half parameter for $books2, that way you get the rest of the array from where $books1 ended...
0
$\begingroup$

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];
        }
    }
?>
$\endgroup$

Comments

0
$\begingroup$

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.

$\endgroup$

Comments

0
$\begingroup$

Use:

$collection->chunk(2);

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

$\endgroup$

Comments

0
$\begingroup$

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]]
$\endgroup$

Comments

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.