PHP code example of germania-kg / filteriterators

1. Go to this page and download the library: Download germania-kg/filteriterators 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/ */

    

germania-kg / filteriterators example snippets



use Germania\FilterIterators\BlankSplitFilterIterator;
 
$items = array(
  [ 'foo' => 'bar john doe'],
  [ 'foo' => 'john bar doe'],
  [ 'foo' => 'john doe bar'],
  [ 'foo' => ['john', 'doe', 'bar']],
);  

// Convert above arrays to StdClasses
$objects = array_map(function ($a) { return (object) $a; }, $items);

$filter = new BlankSplitFilterIterator( $items, "foo", "bar" );
echo iterator_count( $filter ); // 4

$filter = new BlankSplitFilterIterator( $objects, "foo", "bar" );
echo iterator_count( $filter ); // 4


use Germania\FilterIterators\WordRegexFilterIterator;

$items = array(
  [ 'foo' => 'bar john doe'],
  [ 'foo' => 'john bar doe'],
  [ 'foo' => 'john doe bar'],
  [ 'foo' => 'bar,john,doe'],
  [ 'foo' => 'john,bar,doe'],
  [ 'foo' => 'john,doe,bar'],
  [ 'foo' => 'john,doe,white-bar'],
  [ 'foo' => 'john,doe,bar-black']
);

// Convert above arrays to StdClasses
$objects = array_map(function ($a) { return (object) $a; }, $items);

$filter = new WordRegexFilterIterator( $items, "foo", "bar" );
echo iterator_count( $filter ); // 8

$filter = new WordRegexFilterIterator( $objects, "foo", "bar" );
echo iterator_count( $filter ); // 8


use Germania\FilterIterators\KeyNotFalseFilterIterator;

// Items may be Arrays or StdClass objects
$data = [
  // These are not "inactive"
  [ 'name' => 'John', 'active' => true ],
  [ 'name' => 'George' ],

  // but these are:
  [ 'name' => 'Paul',  'active' => false ],
  [ 'name' => 'Ringo', 'active' => 0 ],
];

$filter = new KeyNotFalseFilterIterator( $data, "active" );
foreach( $filter as $item ):
	// John
	// George
	echo $item['name'];
endforeach;


use Germania\FilterIterators\NotEmptyFieldFilterIterator;

// Items may be Arrays or StdClass objects
$data = [
  // These are not "inactive"
  [ 'name' => 'John', 'active' => true ],
  [ 'name' => 'George' ],

  // but these are:
  [ 'name' => 'Paul',  'active' => false ],
  [ 'name' => 'Ringo', 'active' => 0 ],
];

$filter = new NotEmptyFieldFilterIterator( $data, "active" );
foreach( $filter as $item ):
	// John
	echo $item['name'];
endforeach;