PHP code example of baygin / php-search-algorithms

1. Go to this page and download the library: Download baygin/php-search-algorithms 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/ */

    

baygin / php-search-algorithms example snippets


$array = [];

for ($index = 0; $index < 100 * 10000; $index++) {
    $array[] = $index + 1;
}

$search = new BinarySearch();

$search->setCompareCallback(fn ($current, $searchValue) => $current === $searchValue)
    ->setDirectionCallback(fn ($current, $searchValue) => $current < $searchValue)
    ->setArray($array)
    ->setSearchValue(98589)
    ->search();

$foundIndex = $search->getFoundIndex();
$foundValue = $search->getFoundValue();

$array = [];

for ($index = 0; $index < 100 * 10000; $index++) {
    $array[] = [
        "id" => $index + 1,
        "first" => "Baris {$index}",
        "last" => "Manco {$index}",
    ];
}

$search = new BinarySearch();

$search->setCompareCallback(fn ($current, $searchValue) => $current["id"] === $searchValue)
    ->setDirectionCallback(fn ($current, $searchValue) => $current["id"] < $searchValue)
    ->setArray($array)
    ->setSearchValue(81300)
    ->search();

$foundIndex = $search->getFoundIndex();
$foundValue = $search->getFoundValue();


$array = [];

for ($index = 0; $index < 100 * 10000; $index++) {
    $array[] = (object) [
        "id" => $index + 1,
        "first" => "Baris {$index}",
        "last" => "Manco {$index}",
    ];
}

$search = new BinarySearch();
$search->setCompareCallback(fn ($current, $searchValue) => $current->id === $searchValue)
    ->setDirectionCallback(fn ($current, $searchValue) => $current->id < $searchValue)
    ->setArray($array)
    ->setSearchValue(81300)
    ->search();

$foundIndex = $search->getFoundIndex();
$foundValue = $search->getFoundValue();