1. Go to this page and download the library: Download nmarniesse/phindexer 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/ */
nmarniesse / phindexer example snippets
use NMarniesse\Phindexer\Collection\Collection;
$list = [
['name' => 'A', 'color' => 'green', 'price' => 60],
['name' => 'B', 'color' => 'green', 'price' => 80],
['name' => 'C', 'color' => 'blue', 'price' => 10],
['name' => 'D', 'color' => 'green', 'price' => 40],
['name' => 'E', 'color' => 'red', 'price' => 50],
];
$collection = new Collection($list);
$collection->addKeyIndex('color');
$results = $collection->findWhere('color', 'green');
// Results is a new instance of Collection that contains the results
foreach ($results as $result) {
print_r($result);
}
// Create the custom index
$expression_index = new ExpressionIndex(function (array $item) {
return $item['color'] === 'green' && $item['price'] <= 50;
});
// Add the index in your collection
$collection->addExpressionIndex($expression_index);
// Search using the index
$results = $collection->findWhereExpression($expression_index, true);
// Search the items that do not satisfy the expression index
$results = $collection->findWhereExpression($expression_index, false);
use NMarniesse\Phindexer\Collection\Collection;
use NMarniesse\Phindexer\IndexType\ExpressionIndex;
// $list can be an array or an iterator
$list = [
new Planet('Earth', 'Solar system'),
new Planet('Mars', 'Solar system'),
new Planet('Kepler 186-f', 'Kepler 186 system'),
];
$collection = new Collection($list);
// Index with property
$collection->addKeyIndex('system');
$results = $collection->findWhere('system', 'Solar system');
// Index with custom expression
$expression_index = new ExpressionIndex(function (Planet $planet) {
return strpos(strtolower($planet->getSystem()), 'solar') !== false;
});
$collection->addExpressionIndex($expression_index);
$results = $collection->findWhereExpression($expression_index, true);