Guest User

Untitled

a guest
Apr 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. return View::make('showbooks')
  2. ->with('books', $books);
  3.  
  4. $halved = array_chunk($books, ceil(count($books)/2));
  5.  
  6. <?php
  7. $books = array("a", "b", "c", "d", "e","f"); // assuming books is an array like this
  8. $count = count($books); // total count of the array books
  9. $half = $count/2; // half of the total count
  10. $books1 = array_slice($books, 0, $half); // returns first half
  11. $books2 = array_slice($books, $half); // returns second half
  12.  
  13. print_r($books1);
  14. print_r($books2);
  15. ?>
  16.  
  17. <?php
  18. $count = count($books); //getting the count of $books
  19. $mid = round($count/2); //getting the mid point of $books
  20. for($i = 0; $i < $count ; $i++){ //looping through the indexes
  21. if($i <= mid - 1){ //Checking the position of $i and adding the value
  22. $books1[] = $books[$i];
  23. }else{
  24. $books2[] = $books[$i];
  25. }
  26. }
  27. ?>
  28.  
  29. $books = collect($books);
  30.  
  31. $halved = $books->split(ceil($books->count()/2))->toArray();
  32.  
  33. // Result: [[a, b, c], [d, e, f]]
Add Comment
Please, Sign In to add comment