Skip to content

Instantly share code, notes, and snippets.

@senmu
Created November 3, 2014 01:23
Show Gist options
  • Select an option

  • Save senmu/dc7c5aecd3aeed904b95 to your computer and use it in GitHub Desktop.

Select an option

Save senmu/dc7c5aecd3aeed904b95 to your computer and use it in GitHub Desktop.
Print out elements in an alternating fashion from two arrays
<?php
// Sample arrays
$dribbble = array("dribbble item 1", "dribbble item 2", "dribbble item 3");
$instagram = array("instagram 1", "instagram 2", "instagram 3", "instagram 4", "instagram 5");
// Determine maximum size to iterate to
$maxSize = max(count($dribbble), count($instagram));
for ($i = 0; $i < $maxSize; $i++) {
// Check that attempting to access from the dribbble array is not out of
// bounds
if (count($dribbble) > $i) {
// Print out the list item HTML
echo sprintf("<li>%s</li>\n", $dribbble[$i]);
}
// Check that attempting to access from the instagram array is not out of
// bounds
if (count($instagram) > $i) {
// Print out the list item HTML
echo sprintf("<li>%s</li>\n", $instagram[$i]);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment