PHP code example of mkrawczyk / funquery

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

    

mkrawczyk / funquery example snippets



use MKrawczyk\FunQuery\FunQuery;
$data=[];//some array of objects
$result=FunQuery::create($data)->filter(fn($x) => $x->a > 5)->sort(fn($x) => $x->b)->map(fn($x) => $x->c)->slice(2,5)->toArray();


$result=FunQuery::create($data)->filter(fn($x) => $x->a > 5)->sort(fn($x) => $x->b)->map(fn($x) => $x->c)->slice(2,5);
foreach($result as $x){

}



function generator(){
    for ($i = 0; ; $i++) {
        yield $i;
    }
}

$result=FunQuery::create(generator())->filter(fn($x)=>$x%2==0)->limit(10)->toArray();//it will 

$obj1=FunQuery::create([1,2,3,4,5]);
$obj2=FunQuery::create(generator());
$obj3=FunQuery::create(new ArrayIterator([1,2,3,4,5]));

FunQuery::create([1,2,3,4,5])->filter(fn($x) => $x > 2)->toArray() // [3,4,5]

FunQuery::create([5,2,3,1,4])->sort()->toArray() // [1,2,3,4,5]
FunQuery::create([5,2,3,1,4])->sort(fn($x) => -$x)->toArray() // [5,4,3,2,1]

FunQuery::create([1,2,3,4,5])->map(fn($x) => $x * 2)->toArray() // [2,4,6,8,10]

FunQuery::create([1,2,3,4,5])->first() // 1

    FunQuery::create([1,2,3])->firstOrNull() // 1
    FunQuery::create([])->firstOrNull() // null
    

FunQuery::create([1,2,3,4,5])->skip(2)->toArray() // [3,4,5]

FunQuery::create([1,2,3,4,5])->limit(2)->toArray() // [1,2]

FunQuery::create([1,2,3,4,5])->slice(1,2)->toArray() // [2,3]

FunQuery::create([[1,2],[3,4],[5,6]])->flat()->toArray() // [1,2,3,4,5,6]

FunQuery::create([1,2,2,3,3,3])->distinct()->toArray() // [1,2,3]
FunQuery::create([1,2,2,3,3,3])->distinct(fn($x) => $x % 2)->toArray() // [1,2]

FunQuery::create([1,2,3,4,5])->toArray() // [1,2,3,4,5]

FunQuery::create([1,2,3,4,5])->toAssoc(fn($x) => "key$x", fn($x) => $x * 2) // ["key1" => 2, "key2" => 4, "key3" => 6, "key4" => 8, "key5" => 10]

FunQuery::create([1,2,1,3,4])->toAssocArrayIgnoreDuplicates(fn($x) => "key$x", fn($x) => $x * 2) // ["key1" => 2, "key2" => 4, "key3" => 6, "key4" => 8]

FunQuery::create([1,2,3,4,5])->some(fn($x) => $x > 2) // true

FunQuery::create([1,2,3,4,5])->every(fn($x) => $x > 2) // false

FunQuery::create([1,2,3,4,5])->count() // 5

FunQuery::create([1,2,3,4,5])->reduce(fn($acc, $x) => $acc + $x, 0) // 15

FunQuery::create([1,2,3,4,5])->groupBy(fn($x) => $x % 2) // [0 => [2,4], 1 => [1,3,5]]

FunQuery::create([1,2,3,4,5])->reverse() // [5,4,3,2,1]

$obj = FunQuery::create([1,2,3,4,5])->map(fn($x) => someLongRunningFunction($x))->execute();
$result1=$obj->filter(fn($x) => $x > 2)->toArray();
$result2=$obj->filter(fn($x) => $x < 2)->toArray();

FunQuery::create([1,2,3,4,5])->min() // 1
FunQuery::create([1,2,3,4,5])->min(fn($x) => -$x) // 5

FunQuery::create([1,2,3,4,5])->max() // 5
FunQuery::create([1,2,3,4,5])->max(fn($x) => -$x) // 1

FunQuery::create([1,2,3,4,5])->sum() // 15
FunQuery::create([1,2,3,4,5])->sum(fn($x) => $x * 2) // 30

FunQuery::create([1,2,3,4,5])->avg() // 3
FunQuery::create([1,2,3,4,5])->avg(fn($x) => $x * 2) // 6

FunQuery::create([1,2,3])->concat(FunQuery::create([4,5,6]))->toArray() // [1,2,3,4,5,6]