Skip to content

Instantly share code, notes, and snippets.

@daverogers
Created November 4, 2021 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daverogers/3189228715b751c1595c61448a1ecf8d to your computer and use it in GitHub Desktop.
Save daverogers/3189228715b751c1595c61448a1ecf8d to your computer and use it in GitHub Desktop.
PHP Round Robin
<?php
// evenly distribute colors across $return keys
$colors = [ 'red', 'blue', 'green', 'pink' ];
$return = [ 0,1,2,3,4,5,6,7,8,9 ];
foreach($return as $i => $loop) {
$return[$loop] = $colors[$i % count($colors)];
}
return $return;
// output:
// array:10 [
// 0 => "red"
// 1 => "blue"
// 2 => "green"
// 3 => "pink"
// 4 => "red"
// 5 => "blue"
// 6 => "green"
// 7 => "pink"
// 8 => "red"
// 9 => "blue"
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment