PHP code example of arsalanthange / php-collections
1. Go to this page and download the library: Download arsalanthange/php-collections library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
arsalanthange / php-collections example snippets
// Array which is to be converted to a Collection
$array = [5, 'a', 't', 2, 7];
$collection = new Collection($array);
//Sort in Ascending Order
$collection->sort()->all();
//Output: ['a', 't', 2, 5, 7]
//Sort in Descending Order
$collection->sort('DESC')->all();
//Output: [7, 5, 2, 't', 'a']
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
$collection = new Collection($array);
//Page number
$page = 1;
//Number of elements to be displayed on the page
$count = 4
//Paginate the collection
$collection->paginate($page, $count);
/*Output: [
'total' => 20,
'current_page' => 1,
'total_pages' => 5,
'from' => 1,
'to' => 4,
'data' => [1, 2, 3, 4]
]
*/