PHP code example of nickbeen / sequential-rank

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

    

nickbeen / sequential-rank example snippets


$order = [
    'red' => 1,
    'blue' => 2,
    'yellow' => 3,
];

$array = [
    ['yellow', 'blue', 'red'],
    ['red', 'blue', 'yellow'],
];

$seqRank = new SequentialRank($array);
$seqRank->orderBy($order);
$result = $seqRank->get();

$result = [
    '1-2-3', // red, blue, yellow
    '3-2-1', // yellow, blue, red
];

enum Colors: string {
    case RED = 'red';
    case BLUE = 'blue';
    case YELLOW = 'yellow';
    
    public function order(): int {
        return match ($this) {
            self::RED => 1,
            self::BLUE => 2,
            self::YELLOW => 3,
        }
    }
}

$array = [
    ['yellow', 'blue', 'red'],
    ['red', 'blue', 'yellow'],
];

$seqRank = new SequentialRank($array);
$seqRank->orderBy(Colors::class);
$result = $seqRank->get();

$result = [
    '1-2-3', // red, blue, yellow
    '3-2-1', // yellow, blue, red
];

$array = [
    ['yellow', 'blue', 'red'],
    ['red', 'blue', 'yellow'],
];

$seqRank = new SequentialRank($array);
$seqRank->orderBy(null)
$result = $seqRank->get();

$result = [
    'red-blue-yellow',
    'yellow-blue-red',
];

enum Buttons: string {
    case FORWARD = 'forward';
    case DOWN = 'down';
    case BACK = 'back';
    case UP = 'up';
    case X = 'x';
    case Y = 'y';
    case A = 'a';
    case B = 'b';
    
    public function order(): int {
        return match ($this) {
            self::FORWARD => 10,
            self::DOWN => 20,
            self::BACK => 30,
            self::UP => 40,
            self::X => 50,
            self::Y => 60,
            self::A => 70,
            self::B => 80
        }
    }
}

$attacks = [
    ['down', 'forward', 'y'],
    ['forward', 'down', 'y'],
    ['up', 'x'],
    ['x', 'y', 'a'],
    ['forward', 'forward', 'b'],
    ['back', 'a', 'y'],
    ['forward', 'back', 'x'],
    ['back', 'x', 'down', 'y', 'a'],
];

$seqRank = new SequentialRank($attacks);
$seqRank->orderBy(Buttons::class);
$result = $seqRank->get();

$result = [
    '10-10-80', // forward, forward, b
    '10-20-60', // forward, down, y
    '10-30-50', // forward, back, x
    '20-10-60', // down, forward, y
    '30-50-20-60-70', // back, x, down, y ,a
    '30-70-60', // back, a, y
    '40-50', // up, x
    '50-60-70', // x, y, a
];