Beispiel #1
0
Datei: a.php Projekt: ihor/Nspl
    echo sprintf("    %s. %s\n", $id, $user->name);
}
// 8. Create a map (name => age) from users data
$usersAgeByName = indexed($users, propertyGetter('name'), true, propertyGetter('age'));
echo "Users age:\n";
foreach ($usersAgeByName as $name => $age) {
    echo sprintf("    %s is %s y.o.\n", $name, $age);
}
// 9. Get users with unique age (unique values in multidimensional array)
$usersWithUniqueAge = array_values(indexed($users, propertyGetter('age')));
echo "Users with unique age:\n";
foreach ($usersWithUniqueAge as $user) {
    echo sprintf("    %s is %s y.o.\n", $user->name, $user->age);
}
// 10. Group users by age range
$usersByAgeRange = keySorted(indexed($users, function ($user) {
    return floor($user->age / 10) * 10;
}, false));
echo "Users by age range:\n";
foreach ($usersByAgeRange as $age => $usersGroup) {
    echo sprintf("    %s-%s: %s\n", $age, $age + 9, implode(', ', map(propertyGetter('name'), $usersGroup)));
}
// 11. Get all numbers less than 20 which are divisible by 3
$numbers = take(range(3, 20), 20, 3);
echo sprintf("Numbers less than 20 which are divisible by 3: %s\n", implode(', ', $numbers));
// 12. Re-order pets rating
$petsRating = reorder(['dog', 'hamster', 'cat'], 2, 1);
echo "New pets rating:\n";
foreach ($petsRating as $pet) {
    echo sprintf("    %s\n", $pet);
}
Beispiel #2
0
 public function testKeySorted()
 {
     $this->assertEquals(array('a' => 1, 'b' => 2, 'c' => 3), keySorted(array('b' => 2, 'c' => 3, 'a' => 1)));
     $this->assertEquals(array('a' => 1, 'b' => 2, 'c' => 3), keySorted(new \ArrayIterator(array('b' => 2, 'c' => 3, 'a' => 1))));
     $this->assertEquals(array('c' => 3, 'b' => 2, 'a' => 1), keySorted(array('b' => 2, 'c' => 3, 'a' => 1), true));
     $this->assertEquals(array('a' => 1, 'b' => 2, 'c' => 3), call_user_func(keySorted, array('b' => 2, 'c' => 3, 'a' => 1)));
     $this->assertEquals('\\nspl\\a\\keySorted', keySorted);
 }