I have some questions! Who...
class Person
{
public $name;
public $age;
public function __construct(string $name, int $age)
{
$this->name = $name;
$this->age = $age;
}
}
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58),
new Person('Carl', 16),
new Person('Ralph', 12)
];
$peopleOver17 = [];
foreach ($people as $person){
if ($person->age >= 18) {
$peopleOver17[] = $person;
}
}
var_dump($peopleOver17); // The array contains the two person objects (Taylor and Rasmus)
SELECT * FROM people WHERE age >= 18
$name = "PHP Usergroup Dresden"; function greet() { global $name; echo "Hi $name!"; } greet(); // Hi PHP Usergroup Dresden!
$name = "all"; greet(); // Hi all!
function greet($name) : string
{
return "Hi $name!";
}
$name = "PHP Usergroup Dresden";
greet($name); // Hi PHP Usergroup Dresden!
$name = "all";
greet($name); // Hi all!
function peopleOver17($people)
{
$peopleOver17 = [];
foreach ($people as $person) {
if ($person->age >= 18) {
$peopleOver17[] = $person;
}
}
return $peopleOver17;
}
function peopleOverAge($people, $age)
{
$peopleOverAge = [];
foreach ($people as $person) {
if ($person->age >= $age) {
$peopleOverAge[] = $person;
}
}
return $peopleOverAge;
}
function peopleWithNameStartingWithT($people)
{
$result = [];
foreach ($people as $person) {
if (strpos($person->name, 'T') === 0) {
$result[] = $person;
}
}
return $result;
}
$twice = function(callable $f, $v){
return $f($f($v));
};
$add3 = function($v){
return $v + 3;
};
$twice($add3, 7); // 13
$filter = function(callable $f, array $collection) {
$result = [];
//Do sth here
return $result;
};
$filter = function (callable $f, array $collection) {
$result = [];
foreach ($collection as $item) {
if ($f($item)) {
$result[] = $item;
}
}
return $result;
};
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58),
new Person('Carl', 16),
new Person('Ralph', 12)
];
$above18 = function (Person $p) {
return $p->age >= 18;
};
$filter($above18, $people);
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58),
new Person('Carl', 16),
new Person('Ralph', 12)
];
$filter(function (Person $p) {
return $p->age >= 18;
}, $people);
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58),
new Person('Carl', 16),
new Person('Ralph', 12)
];
$olderPeople = array_filter($people, function (Person $p) {
return $p->age >= 18;
}));
Passes each array item to the callback function. If the function returns true, the item is included in the result array.
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58),
new Person('Carl', 16),
new Person('Ralph', 12)
];
$names = [];
foreach($people as $person){
$names[] = $person->name;
}
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58),
new Person('Carl', 16),
new Person('Ralph', 12)
];
array_map(function (Person $p) {
return $p->name;
}), $people); // ['Taylor','Rasmus','Carl','Ralph']
Passes each array item to the callback function. The return value of that function will be included in the result array.
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58)
];
array_walk($people, function (Person $p) {
echo $p->name ." "; // Impure, yeah
})); // 'Taylor Rasmus '
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58)
];
$sumAge = function (array $collection) {
$age = 0;
array_walk($collection, function (Person $p) use (&$age) {
$age += $p->age;
});
return $age;
};
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58)
];
$sumAge = function ($accumulatedAge, Person $person) {
return $accumulatedAge + $person->age;
};
array_reduce($people, $sumAge); //End result: 93
Passes each array item to the callback function. The return value of that function will be used as first parameter (accumulator) in the next call. Does return whatever you like to.
$people = [
new Person('Taylor', 35),
new Person('Rasmus', 58)
];
$concatNames = function (string $names, Person $person) {
return $names . $person->name . " ";
};
array_reduce($people, $concatNames, ""); // "Taylor Rasmus "
Takes initial accumulator value as 3rd argument (optional).
$ages = [];
foreach ($people as $person) {
if (strpos($person->name, "a") !== false) {
$ages[] = $person->age;
}
}
$result = array_sum($ages) / count($ages);
$nameContains = function (string $toContain) { return function (Person $p) use ($toContain) { return strpos($p->name, $toContain) !== false; }; };
$result = collect($people) ->filter($nameContains("a")) ->pluck('age') // maps to the age property ->avg();