PHP code example of midnite81 / loopy

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

    

midnite81 / loopy example snippets


use function Midnite81\Loopy\each;

$colours = [
    'blue',
    'red',
    'green',
    'yellow'
];

each($colours, function($colour, $key) {
    echo $colour . " is at index " . $key . "\n";
});

use function Midnite81\Loopy\all;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = all($employees, fn($employee) => $employee['name'] === 'bob');
// please note the key is also passed to the closure; therefore if the key is necessary
// to your function you could for example do the following 
// $allBobs = all($employees, fn($employee, $key) => $employee['name'] === 'bob' && $key != 'id000');


$allBobs = false;
// thankfully, not everyone in the department is called bob

use function Midnite81\Loopy\some;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = some($employees, fn($employee) => $employee['name'] === 'bob');

// please note the key is also passed to the closure; therefore if the key is necessary
// to your function you could for example do the following 
// $allBobs = some($employees, fn($employee, $key) => $employee['name'] === 'bob' && $key != 'id000');


$allBobs = true;
// one or more people in the department are called bob

use function Midnite81\Loopy\map;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = map($employees, fn($employee, $key) => $employee['name']');

$allBobs = [
    'bob',
    'dave',
    'susan',
]

use function Midnite81\Loopy\reduce;

$moneyReceived = [
    20.00,
    3.92,
    3.01,
    27.00
];

$totalMoneyReceived = reduce($moneyReceived, fn($current, $value, $key) => (float)$current + $value);
// $current is the current value of the reducer

$totalMoneyReceived = 53.93;

use function Midnite81\Loopy\filter;

$users = [
            ["name" => 'dave'],
            ["name" => 'susan'],
            ["name" => 'ingrid'],
            ["name" => 'patricia'],
            ["name" => 'sally'],
        ];

$usersWhoseNamesDontStartWithS = filter($users, fn($user) => !str_starts_with($user['name'], "s"));


$usersWhoseNamesDontStartWithS = [
    'dave',
    'ingrid',
    'patricia'
]

use function Midnite81\Loopy\times;

$peopleOnTheBus = [
    'andy',
    'bob',
    'sally',
    'wendy',
    'bob'
];

$areThereTwoBobsOnTheBus = times($peopleOnTheBus, fn($people) => $people === 'bob', 2);

$areThereTwoBobsOnTheBus = true;

use function Midnite81\Loopy\once;

$peopleOnTheBus = [
    'andy',
    'bob',
    'sally',
    'wendy'
];

$isThereJustOneAndyOnTheBus = once($peopleOnTheBus, fn($people) => $people === 'andy');

$isThereJustOneAndyOnTheBus = true;