PHP code example of carloswph / foreacher

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

    

carloswph / foreacher example snippets


use Foreacher\Iterator;

 3, 5, 6, 7, 11, 15, 24, 34, 56, 66, 77, 124],
	'2' => [3, 455, 5, 89, 72, 2434, 1, 1233, 4]
];

$iterator = new Iterator($entries);

function echoR($item)
{
	echo $item . PHP_EOL;
	echo $item + 4 . PHP_EOL;
}

$iterator->limitFirst('1', 4, 'echoR');

// Returns: 1 5 3 7 5 9 6 10

$iterator->limitLast('1', 4, 'echoR');

// Returns: 56 60 66 70 77 81 124 128

$iterator->limitFrom('1', 4, 'echoR');

// Returns: 7 11 11 15 15 19 24 28 34 38 56 60 66 70 77 81 124 128

function echoR($item)
{
	echo $item . PHP_EOL;
}

$iterator->looping('1', 2.5, 'echoR');

// Returns: 1 3 5 6 7 11 15 24 34 56 66 77 124 1 3 5 6 7 11 15 24 34 56 66 77 124 1 3 5 6 7 11

function echoR($item)
{
	var_dump($item);
}

$iterator->loopAll('echoR');

/* Returns:

array(2) {
  [1]=>
  int(1)
  [2]=>
  int(3)
}
array(2) {
  [1]=>
  int(3)
  [2]=>
  int(455)
}
array(2) {
  [1]=>
  int(5)
  [2]=>
  int(5)
} ... and so on...
*/ 


function echoR($item)
{
  echo $item . PHP_EOL;
}

$iterator->filterValues('1', [1, '3', 11], 'echoR');
// Returns: 5 6 7 15 24 34 56 66 77 124

$iterator->filterKeys('1', [1, '3', 11], 'echoR');
// Returns: 1 5 7 11 15 24 34 56 66 124


function echoR($item)
{
  echo $item . PHP_EOL;
}

$iterator->regexMatch('1', "/^[1-3]+[0-3]*$/", 'echoR');
// Returns: 1 3 11