Last active
August 29, 2015 13:59
-
-
Save necenzurat/10769687 to your computer and use it in GitHub Desktop.
insert element to array every x position
This file contains hidden or 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 | |
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