PHP code example of rmtram / sorter

1. Go to this page and download the library: Download rmtram/sorter 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/ */

    

rmtram / sorter example snippets


$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$sorter = Rmtram\Sorter\Sorter::make($items);


$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::runSort($items, ['id' => 'asc'], ['id'], 1, 1);

var_dump($results);

$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::make($items)->sort(['id' => 'asc']);

var_dump($results);

$items = [
    ['id' => 1, 'name' => 'b', 'age' =>  9, 'created_at' => '2015-10-10 10:10:00'],
    ['id' => 2, 'name' => 'a', 'age' =>  9, 'created_at' => '2015-10-10 10:10:10'],
    ['id' => 3, 'name' => 'z', 'age' =>  3, 'created_at' => '2015-10-10 10:10:20'],
    ['id' => 5, 'name' => 'f', 'age' => 11, 'created_at' => '2015-10-10 10:10:15'],
    ['id' => 4, 'name' => 'e', 'age' => 16, 'created_at' => '2015-10-10 10:10:20'],
    ['id' => 6, 'name' => 'o', 'age' => 15, 'created_at' => '2015-10-10 10:10:05']
];

$results = Rmtram\Sorter\Sorter::make($items)->sort([
    'age'        => 'asc',
    'created_at' => 'asc',
    'id'         => 'desc'
]);

var_dump($results);

$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::make($items)->refuse('age')->sort(['id' => 'asc']);

var_dump($results);

$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 1, 'name' => 'bac', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$sortedItems = Rmtram\Sorter\Sorter::make($items)->select(['id', 'name'])->sort(['id' => 'asc', 'name' => 'desc']);

var_dump($sortedItems);

$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::make($items)
    ->offset(3)
    ->sort(['id' => 'asc']);

var_dump($results);

$items = [
    ['id' => 1, 'name' => 'abc', 'created_at' => '2015-10-14 10:10:01'],
    ['id' => 2, 'name' => 'def', 'created_at' => '2015-10-14 10:10:05'],
    ['id' => 5, 'name' => 'mno', 'created_at' => '2015-10-14 10:10:39'],
    ['id' => 3, 'name' => 'ghi', 'created_at' => '2015-10-14 10:10:09']
];

$results = Rmtram\Sorter\Sorter::make($items)
    ->limit(1)
    ->sort(['id' => 'asc']);

var_dump($results);

array(1) {
  [0]=>
  array(1) {
    ["id"]=>
    int(2)
  }
}

array(4) {
  [0]=>
  array(1) {
    ["id"]=>
    int(1)
  }
  [1]=>
  array(1) {
    ["id"]=>
    int(2)
  }
  [2]=>
  array(1) {
    ["id"]=>
    int(3)
  }
  [3]=>
  array(1) {
    ["id"]=>
    int(5)
  }
}

array(1) {
  [0]=>
  array(3) {
    ["id"]=>
    int(5)
    ["name"]=>
    string(3) "mno"
    ["created_at"]=>
    string(19) "2015-10-14 10:10:39"
  }
}

array(1) {
  [0]=>
  array(3) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(3) "abc"
    ["created_at"]=>
    string(19) "2015-10-14 10:10:01"
  }
}