Skip to content

Instantly share code, notes, and snippets.

@felds
Last active December 14, 2015 21:39
Show Gist options
  • Select an option

  • Save felds/5152637 to your computer and use it in GitHub Desktop.

Select an option

Save felds/5152637 to your computer and use it in GitHub Desktop.
Exemplo de "list comprehension" em PHP
#!/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