PHP code example of samsonasik / array-lookup

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

    

samsonasik / array-lookup example snippets


use ArrayLookup\AtLeast;

$data = [1, 2, 3];
$filter = static fn($datum): bool => $datum === 1;

var_dump(AtLeast::once($data, $filter)) // true

$data = [1, 2, 3];
$filter = static fn($datum): bool => $datum === 4;

var_dump(AtLeast::once($data, $filter)) // false

// WITH key array 

use ArrayLookup\AtLeast;

$data = [1, "1", 3];
$filter = static fn($datum): bool => $datum == 1;

var_dump(AtLeast::twice($data, $filter)) // true

$data = [1, "1", 3];
$filter = static fn($datum): bool => $datum === 1;

var_dump(AtLeast::twice($data, $filter)) // false

// WITH key array 

use ArrayLookup\AtLeast;

$data = [false, null, 0];
$filter = static fn($datum): bool => ! $datum;
$times = 3;

var_dump(AtLeast::times($data, $filter, $times)) // true

$data = [1, null, 0];
$filter = static fn($datum): bool => ! $datum;
$times = 3;

var_dump(AtLeast::times($data, $filter, $times)) // false

// WITH key array 

use ArrayLookup\Only;

$data = [1, 2, 3];
$filter = static fn($datum): bool => $datum === 1;

var_dump(Only::once($data, $filter)) // true


$data = [1, "1", 3]
$filter = static fn($datum): bool => $datum == 1;

var_dump(Only::once($data, $filter)) // false

// WITH key array 

use ArrayLookup\Only;

$data = [1, "1", 3];
$filter = static fn($datum): bool => $datum == 1;

var_dump(Only::twice($data, $filter)) // true

$data = [true, 1, new stdClass()];
$filter = static fn($datum): bool => (bool) $datum;

var_dump(Only::twice($data, $filter)) // false

// WITH key array 

use ArrayLookup\Only;

$data = [false, null, 1];
$filter = static fn($datum): bool => ! $datum;
$times = 2;

var_dump(Only::times($data, $filter, $times)) // true


$data = [false, null, 0];
$filter = static fn($datum): bool => ! $datum;
$times = 2;

var_dump(Only::times($data, $filter, $times)) // false

// WITH key array 

use ArrayLookup\Finder;

$data = [1, 2, 3];
$filter = static fn($datum): bool => $datum === 1;

var_dump(Finder::first($data, $filter)) // 1

$filter = static fn($datum): bool => $datum == 1000;
var_dump(Finder::first($data, $filter)) // null

// RETURN the Array key, pass true to 3rd arg

$filter = static fn($datum): bool => $datum === 1;

var_dump(Finder::first($data, $filter, true)) // 0

$filter = static fn($datum): bool => $datum == 1000;
var_dump(Finder::first($data, $filter, true)) // null

// WITH key array 

use ArrayLookup\Finder;

$data = [6, 7, 8, 9];
var_dump(Finder::last(
    $data,
    static fn ($datum): bool => $datum > 5
)); // 9

var_dump(Finder::last(
    $data,
    static fn ($datum): bool => $datum < 5
)); // null

// RETURN the Array key, pass true to 3rd arg

// ... with PRESERVE original key
var_dump(Finder::last(
    $data,
    static fn ($datum): bool => $datum > 5,
    true
)); // 3

// ... with RESORT key, first key is last record
var_dump(Finder::last(
    $data,
    static fn ($datum): bool => $datum > 5,
    true,
    false
)); // 0

var_dump(Finder::last(
    $data,
    static fn ($datum): bool => $datum < 5,
    true
)); // null

// WITH key array 

use ArrayLookup\Finder;

$data = [6, 7, 8, 9];
var_dump(Finder::rows(
    $data,
    static fn($datum): bool => $datum > 6
)); // [7, 8, 9]

var_dump(Finder::rows(
    $data,
    static fn ($datum): bool => $datum < 5
)); // []

// ... with PRESERVE original key
var_dump(Finder::rows(
    $data,
    static fn ($datum): bool => $datum > 6,
    true
)); // [1 => 7, 2 => 8, 3 => 9]

var_dump(Finder::rows(
    $data,
    static fn ($datum): bool => $datum < 5,
    true
)); // []

// WITH key array 

$newArray = [];

foreach ($data as $datum) {
    if (is_string($datum)) {
        $newArray[] = trim($datum);
    }
}

use ArrayLookup\Collector;

$when = fn ($datum): bool => is_string($datum);
$limit = 2;
$transform = fn ($datum): string => trim($datum);

$newArray = Collector::setUp($data)
       ->when($when) // optional, can just transform without filtering
       ->withLimit(2) // optional to only collect some data provided by limit config
       ->withTransform($transform)
       ->getResults();