PHP code example of obernard / property-indexer
1. Go to this page and download the library: Download obernard/property-indexer 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/ */
obernard / property-indexer example snippets
$obj1->id == "id1";
$obj1->value == "value1";
$obj2->id == "id2";
$obj2->value == "value2";
$indexer = new Obernard\PropertyIndexer\PropertyIndexer('id', 'value');
$indexer->add($obj1)->add($obj2);
$index->get('id1') // returns "value1"
$index->get('id2') // returns "value2"
$index->get($obj2) // returns "value2"
$array1= ["id" => "id1", "value" => "value1"];
$array2 = ["id" => "id2", "value" => "value2"];
$indexer = new Obernard\PropertyIndexer\PropertyIndexer('[id]', '[value]');
$indexer->add($array1)->add($array2);
$index->get('id1') // returns "value1"
$index->get('id2') // returns "value2"
$indexer = new Obernard\PropertyIndexer\PropertyIndexer('[id]', '[value]');
$collection = [
["id" => "id1", "value" => "value1"],
["id" => "id2", "value" => "value2"]
]
$indexer = $indexer->load($collection);
$index->get('id1') // returns value1
$index->get('id2') // returns value2
$indexer = new Obernard\PropertyIndexer\PropertyIndexer('[id]', '[value]', $collection);
$objectIndexer = new Obernard\PropertyIndexer\PropertyIndexer('id');
$arrayIndexer = new Obernard\PropertyIndexer\PropertyIndexer('[id]', null);
$obj1->id == "id1";
$obj1->value == "value1";
$obj1->date == "today";
$obj2->id == "id2";
$obj2->value == "value2";
$obj2->date == "today";
// first arg is the collection of objects
// second arg is the "leaves" value
// third arg is a groupBy-like definition of the tree levels
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], 'value', ['id', 'date']);
// ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=>
// array(2) {
// ["id1"]=>
// array(1) {
// ["today"]=>
// string(6) "value1"
// }
// ["id2"]=>
// array(1) {
// ["today"]=>
// string(6) "value2"
// }
// }
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], 'value', ['date', 'id']);
// ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=>
// array(1) {
// ["today"]=>
// array(2) {
// ["id1"]=>
// string(6) "value1"
// ["id2"]=>
// string(6) "value2"
// }
// }
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], 'value', ['date'], PropertyTree::ARRAY_LEAF);
// ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=>
// array(1) {
// ["today"]=>
// array(2) {
// [0]=>
// string(6) "value1"
// [1]=>
// string(6) "value2"
// }
// }
// Use an Idendity Closure to retrieve Objects themselves:
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], function($item):string {return $item;}, ['id']);
["tree":"Obernard\PropertyIndexer\PropertyTree":private]=>
// array(2) {
// ["id1"]=>
// object(stdClass)#106 (3) {
// ["id"]=>
// string(3) "id1"
// ["value"]=>
// string(6) "value1"
// ["date"]=>
// string(5) "today"
// }
// ["id2"]=>
// object(stdClass)#83 (3) {
// ["id"]=>
// string(3) "id2"
// ["value"]=>
// string(6) "value2"
// ["date"]=>
// string(5) "today"
// }
// }
// Or a simple way :
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], null, ['id']);
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], null, [function($item):string {return $item->id;}]);
// is a complicated way to get the same resulting tree as :
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], null, ['id']);
php
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], 'value', ['date']);
// ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=>
// array(1) {
// ["today"]=>
// string(6) "value2"
// }