baygin/php-search-algorithms

The search algorithms implementation for the php arrays that usable as a composer package

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/baygin/php-search-algorithms

1.1.0 2023-04-29 22:59 UTC

This package is auto-updated.

Last update: 2025-10-29 03:18:34 UTC


README

The search algorithm implementation in PHP 8 to find the value with the query as fast.

Usage

Install

Git Clone

git clone https://github.com/baygin/php-search-algorithms.git

Composer

composer require baygin/php-search-algorithms

Binary Search

Array

$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();

Arrays in array

$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();

Objects in array

$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();

Tests

composer test

Contributing

If you want to contribute to the development of this library, you can open an issue or submit a pull request.

License

Licensed under the GPL3. See LICENSE for more information.