Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- return View::make('showbooks')
- ->with('books', $books);
- $halved = array_chunk($books, ceil(count($books)/2));
- <?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);
- ?>
- <?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];
- }
- }
- ?>
- $books = collect($books);
- $halved = $books->split(ceil($books->count()/2))->toArray();
- // Result: [[a, b, c], [d, e, f]]
Add Comment
Please, Sign In to add comment