Last active
December 14, 2015 21:39
-
-
Save felds/5152637 to your computer and use it in GitHub Desktop.
Exemplo de "list comprehension" em PHP
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
| #!/usr/bin/env php | |
| <?php | |
| $a = range(0, 20); | |
| $divisibleBy = 3; | |
| // array_values: normalize the retuning indices | |
| // array_filter: perform a select | |
| $b = array_values(array_filter($a, function ($value) use ($divisibleBy) { | |
| return (($value % $divisibleBy) == 0); | |
| })); | |
| var_dump($b); | |
| // Output: | |
| // | |
| // array(7) { | |
| // [0] => | |
| // int(0) | |
| // [1] => | |
| // int(3) | |
| // [2] => | |
| // int(6) | |
| // [3] => | |
| // int(9) | |
| // [4] => | |
| // int(12) | |
| // [5] => | |
| // int(15) | |
| // [6] => | |
| // int(18) | |
| // } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment