Skip to content

Instantly share code, notes, and snippets.

@necenzurat
Last active August 29, 2015 13:59
Show Gist options
  • Save necenzurat/10769687 to your computer and use it in GitHub Desktop.
Save necenzurat/10769687 to your computer and use it in GitHub Desktop.
insert element to array every x position
<?php
function insertEvery($array, $element = '', $every = null){
if ($every == null) return false;
$index = 0;
foreach($array as $value) {
if (($index + 1) % $every == 0) {
array_splice($array, $index, 0, $element);
}
$index++;
}
return $array;
}
echo "<pre>";
$array = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15");
var_dump(insertEvery($array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment