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); }
public function testTake() { $this->assertEquals([1, 2, 3], take([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)); $this->assertEquals([1, 2, 3], take(new \ArrayIterator([1, 2, 3, 4, 5, 6, 7, 8, 9]), 3)); $this->assertEquals([1, 3, 5], take([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 2)); $this->assertEquals([1, 3, 5], take(new \ArrayIterator([1, 2, 3, 4, 5, 6, 7, 8, 9]), 3, 2)); $this->assertEquals([1, 4, 7], take([1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 3)); $this->assertEquals([], take([1, 2, 3, 4, 5, 6, 7, 8, 9], 0)); $this->assertEquals([], take([], 3)); $this->assertEquals([], take([], 3, 2)); $this->assertEquals([1, 2, 3], call_user_func(take, [1, 2, 3, 4, 5, 6, 7, 8, 9], 3)); $this->assertEquals('\\nspl\\a\\take', take); }