PHP code example of nmarniesse / phindexer

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

use NMarniesse\Phindexer\Collection\Collection;

$collection = new Collection([]);
$collection->addKeyIndex('system');

$collection->addItem(new Planet('Earth', 'Solar system'));
$collection->addItem(new Planet('Mars', 'Solar system'));
$collection->addItem(new Planet('Kepler 186-f', 'Kepler 186 system'));

$results = $collection->findWhere('system', 'Solar system');

use NMarniesse\Phindexer\Collection\Collection;
use Symfony\Component\Validator\Constraints as Assert;

// Valiation ok.
$constraint = new Assert\Type(['type' => Planet::class]);
$collection = new Collection([new Planet('Earth', 'Solar system')], $constraint);

// Valiation ok.
$collection->addItem(new Planet('Mars', 'Solar system'));
$collection->addItem(new Planet('Kepler 186-f', 'Kepler 186 system'));

// Valiation fails.
$collection->addItem(new Star('Sun'));