PHP code example of faisalrehmanid / fr-binary-search

1. Go to this page and download the library: Download faisalrehmanid/fr-binary-search 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/ */

    

faisalrehmanid / fr-binary-search example snippets


// What value to search e.g. '2'
$needle = '2';

// Search from array
$haystack = [
    ['id' => '1', 'name' => 'name_1'],
    ['id' => '2', 'name' => 'name_2'],
    ['id' => '3', 'name' => 'name_3']
];

// Search key from array. e.g. 'id' must be ASC sorted
$key = 'id';

// $results always will be array
$results = \FR\BinarySearch\BinarySearchUtil::binarySearch($needle, $haystack, $key);

echo '<pre>';
    print_r($results);
echo '</pre>';