Last active
May 18, 2023 00:35
-
-
Save rotelstift/06df5b80799b19df2aa0b53d9a991931 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$numbers = range(0, 9); | |
// 0を除いた偶数だけ欲しい | |
$evenNumbers = array_filter( | |
// https://www.php.net/manual/ja/function.array-filter.php | |
// 上記の例2を参照 | |
array_filter($numbers), | |
fn($n) => $n % 2 === 0 | |
); | |
print_r($evenNumbers); | |
// 0を除いた偶数だけ2乗したい | |
$squareEven = array_map( | |
fn($n) => $n * $n, | |
array_filter( | |
array_filter($numbers), | |
fn($n) => $n % 2 === 0 | |
) | |
); | |
print_r($squareEven); | |
// 偶数だけ2乗したものを計算式とともに出力する | |
array_walk( | |
// ここにArrayを返す関数を置くと警告が出る | |
// array_walkの第一引数はリファレンス渡しを期待しているからである | |
// https://www.php.net/manual/ja/functions.arguments.php#functions.arguments.by-reference | |
$squareEven, | |
fn($value, $key) => print("{$key} * {$key} = {$value}\n") | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment