Skip to content

Instantly share code, notes, and snippets.

@Pushplaybang
Created April 22, 2013 05:49
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 Pushplaybang/5432650 to your computer and use it in GitHub Desktop.
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
// 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