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]
]
*/

$array = [
    ['apples' => 5, 'bananas' => 3, 'oranges' => 2],
    ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
];

$collection = new Collection($array);

//Filter the collection
$collection->where('apples', '>', 5)->all();

/*
Output: [
    ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
]
*/

//Filter the collection
$collection->where('apples', '>', 5)->where('bananas', '=', 10)->all();

/*
Output: [
    ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
]
*/

$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$collection = new Collection($array);

//Count the number of elements
$collection->count();

//Output: 10

$array = [
    'first_basket' => ['apples' => 5, 'bananas' => 3, 'oranges' => 2],
    'second_basket' => ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    'third_basket' => ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
];

$collection = new Collection($array);

$collection->values()->all();

/*Output: [
    ['apples' => 5, 'bananas' => 3, 'oranges' => 2],
    ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
]
*/

$array = [
    'first_basket' => ['apples' => 5, 'bananas' => 3, 'oranges' => 2],
    'second_basket' => ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    'third_basket' => ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
];

$collection = new Collection($array);

$collection->get('first_basket');

// Output: ['apples' => 5, 'bananas' => 3, 'oranges' => 2]

$collection->get('fourth_basket');

// Output: null

//Second parameter is the default value if the key does not exist
$collection->get('fourth_basket', 'No fruits here!');

// Output: No fruits here!


$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$collection = new Collection($array);

//Sum elments
$collection->sum();

//Output: 55

$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$collection = new Collection($array);

//Average elements
$collection->avg();

//Output: 5.5

$array = [1, 2, 3, 4];

$collection = new Collection($array);

//Join elements
$collection->implode();

//Output: 1,2,3,4

//Join elements
$collection->implode('-');

//Output: 1-2-3-4

$array = [
    [ 'foo' => 1, 'bar' => 2],
    [ 'foo' => 3, 'bar' => 4],
    [ 'foo' => 5, 'bar' => 6],
];

$collection = new Collection($array);

//Join elements
$collection->implode(',', 'foo');

//Output: 1,3,5