PHP code example of bentools / picker

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

    

bentools / picker example snippets


use BenTools\Picker\Picker;

   'foo',
        80,
    ],
    [
        'bar',
        60,
    ],
    [
        'baz',
        5,
    ],
];

$picker = Picker::create();
foreach ($collection as $key => [$value, $weight]) {
    $picker = $picker->withItem($value, $weight);
}

echo $picker->pick(); // Will be mostly foo or bar

$picker = Picker::create()->withItems(['foo', 'bar', 'baz']);
echo $picker->pick(); // Will be a truly random value between foo, bar and baz

$picker = Picker::create(shift: true)->withItems(['foo', 'bar']);
$picker->pick(); // let's assume `foo` is picked
$picker->pick(); // only `bar` remains
$picker->pick(); // RuntimeException