1

I have a following array in PHP :

$myArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o"];
  1. I would like to insert a new element (e.g. 1 after every fourth element in the same array.
  2. Also, the array will start with 1.

The output will be:

$myTest = [1, "a", "b", "c", "d", 1, "e", "f", "g", "h", 1, "i", "j",
"k", "l", 1, "m", "n", "o"];

Note that in the above array, there is a 1 added after every 4th element.

Also, I am not adding the element just once. It is being added repeatedly after every 4th element until the end of the array.

codeonion
  • 216
  • 2
  • 14
  • 1
    possible duplicate of [Insert new item in array on any position in PHP](http://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php) – Sjoerd Jun 03 '15 at 11:28
  • 1
    This question has a different purpose. I am trying to insert an element after every 4th element in the array. I am not willing to insert an element once. The element is to be inserted repeatedly until the end of the array. – codeonion Jun 03 '15 at 11:51
  • second answer would be useful – PHP dev Jun 03 '15 at 11:56
  • 1
    It is a good answer! But The array also has to start with a "1". And the question is "not" a duplicate of http://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php – codeonion Jun 03 '15 at 11:58

3 Answers3

4

You can use the modulo operator associated with the function array_splice to reach your goal.

E.G:

<?php
$myTest= ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"];
$added = 0;
for($i=1;$i<count($myTest);$i++){
   if($i%4 == 0){
      $temp = array(1);
      array_splice($myTest, ($i+$added), 0, $temp );
   }
}
print_r($myTest);
?>

DEMO

Fred B.
  • 1,331
  • 9
  • 9
3

I think this is what you are looking for:

<?php
$myArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"];
$myTest = [1, "a", "b", "c", "d", 1, "e", "f", "g", "h", 1, "i", "j",
"k", "l", 1, "m", "n", "o"];
for($i = 0; $i < count($myArray); $i+=4)
{
    array_splice($myArray, $i, 0, [1]);
    $i++;
}
print_r($myArray);
echo ('<br>'. ($myArray==$myTest));
?>

Output:

[1, "a", "b", "c", "d", 1, "e", "f", "g", "h", 1, "i", "j",
"k", "l", 1, "m", "n", "o"]
1

EDIT: Additional requirements

fsacer
  • 1,352
  • 1
  • 14
  • 21
  • You might want to use $i+=5 but I did it like that to make it clear the array is bigger. – fsacer Jun 03 '15 at 12:08
  • Please change the output's inserted element value from "1" to 1. The answer is very good. – codeonion Jun 03 '15 at 12:14
  • 1
    I changed that but PHP doesn't consider because of implicit type conversions in PHP. http://php.net/manual/en/language.types.type-juggling.php – fsacer Jun 03 '15 at 12:18
  • @fsacer may i know the meaning of [1] in this line array_splice($myArray, $i, 0, [1]);? – PHP dev Jun 03 '15 at 12:20
  • 1
    array_splice expects array as replacement http://php.net/manual/en/function.array-splice.php – fsacer Jun 03 '15 at 12:21
  • 1
    actually you could have just 1 instead of [1] – fsacer Jun 03 '15 at 12:23
  • Thanks a lot for this answer! This does exactly what I want. I have made a function out of this answer and now it is adding my provided values to multiple arrays having lengths up to 1000s of elements. I am just hoping to not see the memory errors. – codeonion Jun 03 '15 at 12:47
2

You can use array_splice function

$myArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"];
$add = array(1);
for($i = 0; $i < count($myArray); $i+=4)
{
    array_splice($myArray, $i, 0, $add);
    $i++;
}
print_r($myArray);
Narendrasingh Sisodia
  • 19,948
  • 5
  • 40
  • 50