The search algorithm implementation in PHP 8 to find the value with the query as fast.
git clone https://github.com/baygin/php-search-algorithms.git
composer require baygin/php-search-algorithms
$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();
composer test
If you want to contribute to the development of this library, you can open an issue or submit a pull request.
Licensed under the GPL3. See LICENSE for more information.