Created
April 22, 2013 05:49
-
-
Save Pushplaybang/5432650 to your computer and use it in GitHub Desktop.
compare 2 php arrays and duplicate or reduce the values of the second so they match
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
// only if the two arrays don't hold the same number of elements | |
if (count($data) != count($colors)) { | |
// handle if $colors is less than $data | |
while (count($colors) < count($data)) { | |
// NOTE : we are using array_values($colors) to make sure we use | |
// numeric keys. | |
// See http://php.net/manual/en/function.array-merge.php) | |
$colors = array_merge($colors, array_values($colors)); | |
} | |
// handle if $colors has more than $data | |
$colors = array_slice($colors, 0, count($data)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment