Created
November 4, 2021 15:29
-
-
Save daverogers/3189228715b751c1595c61448a1ecf8d to your computer and use it in GitHub Desktop.
PHP Round Robin
This file contains 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 | |
// 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