PHP code example of codewithkyrian / laravel_position

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

    

codewithkyrian / laravel_position example snippets


return [
    /**
     * The default key to use when adding the position to each item
     * in the collection. It could still be overridden when calling
     * method by passing a string as the second argument.
     */
    'key' => 'position',

    /**
     * Whether to show the total number of items in the collection
     * in the position text eg. 12th out of 30
     */
    'show_total' => false,

    /**
     * The text to join the position and the total number of items
     * in the collection
     */
    'join_text' => 'out of'
];

$items = collect(
    [
        [ 'name' => 'Mr A', 'score' => 100 ],
        [ 'name' => 'Mr B', 'score' => 74 ],
        [ 'name' => 'Mr C', 'score' => 60 ],
        [ 'name' => 'Mr D', 'score' => 83 ],
        [ 'name' => 'Mr E', 'score' => 89 ],
    ]
);
$rankedItems = $items->rankBy('score');
dd($rankedItems->toArray());

/*
array:5 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "score" => 100
    "position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr E"
    "score" => 89
    "position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr D"
    "score" => 83
    "position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr B"
    "score" => 74
    "position" => "5th"
  ]
  4 => array:3 [▼
    "name" => "Mr C"
    "score" => 60
    "position" => "6th"
  ]
]
*/

$users = collect(
    [
        [ 
            'name' => 'Mr A', 
            'result' => [
                'score' => 100, 'status' => 1
            ]
        ],
        [ 
            'name' => 'Mr B', 
            'result' => [
                'score' => 74, 'status' => 1
            ]
        ],
        [ 
            'name' => 'Mr C', 
            'result' => [
                'score' => 60, 'status' => 1
            ]
        ],
        [ 
            'name' => 'Mr D', 
            'result' => [
                'score' => 83, 'status' => 1
            ]
        ]
    ]
);
$rankedUsers = $users->rankBy('result.score');
echo ($rankedUsers->toArray());

/*
array:4 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "result" => array:2 [▼
      "score" => 100
      "status" => 1
    ]
    "position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr D"
    "result" => array:2 [▼
      "score" => 83
      "status" => 1
    ]
    "position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr B"
    "result" => array:2 [▼
      "score" => 74
      "status" => 1
    ]
    "position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr C"
    "result" => array:2 [▼
      "score" => 60
      "status" => 1
    ]
    "position" => "5th"
  ]
]
*/


$rankedItems = $items->rankBy('score', 'class_position');
echo ($rankedItems->toArray());

/*
array:5 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "score" => 100
    "class_position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr E"
    "score" => 89
    "class_position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr D"
    "score" => 83
    "class_position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr B"
    "score" => 74
    "class_position" => "5th"
  ]
  4 => array:3 [▼
    "name" => "Mr C"
    "score" => 60
    "class_position" => "6th"
  ]
]
*/