Skip to content

Instantly share code, notes, and snippets.

@mudge
Created July 17, 2014 14:32
Show Gist options
  • Save mudge/3c1ac272f9856c1d5bc2 to your computer and use it in GitHub Desktop.
Save mudge/3c1ac272f9856c1d5bc2 to your computer and use it in GitHub Desktop.
Iterate over an array as a sliding window of element pairs.
<?php
$a = array(1, 2, 3);
$pairs = array_map(null, $a, array_slice($a, 1));
/**
* Prints:
* (1,2)(2, 3)(3, )
*/
foreach ($pairs as list($x, $y)) {
echo "({$x}, {$y})";
}
@antanas-arvasevicius
Copy link

+1

@gitRanie
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment