0

I want to add new Item after every 3 indexes.

$mainArr = '[
    {"name":"Saqib1", "id":"1"},
    {"name":"Saqib2", "id":"2"},
    {"name":"Saqib3", "id":"3"},
    {"name":"Saqib4", "id":"4"},
    {"name":"Saqib5", "id":"5"},
    {"name":"Saqib6", "id":"6"},
    {"name":"Saqib7", "id":"7"},
    {"name":"Saqib8", "id":"8"},
    {"name":"Saqib9", "id":"9"},
    {"name":"Saqib10", "id":"10"}
]';


// Initial array of objects
$newObject = new stdClass(); // The new object you want to add

$newObject->name = 'New';
$newObject->id = '11';
$newObject->extraKey = 'this is extra key for newObjects to identify';

$n = 3; // The number of indexes after which to add the new object

$decoded = json_decode($mainArr);

for ($i = 0; $i < count($decoded); $i++) {
     
    if (($i + 1) % $n == 0) {
        array_splice($decoded, $i, 0, $newObject);
    }
}

print_r($mainArr);

Error:

PHP Warning: count(): Parameter must be an array or an object that implements Countable.

I tried json_decode still not working.

UPDATE: The valid json issue resolved as indicated in comments.

Najam Us Saqib
  • 3,190
  • 1
  • 24
  • 43
  • `I tried json_decode`...but it's not valid JSON currently, so you can't use that. Valid JSON has double-quotes for property names and string values, not single quotes. Sites like https://jsonlint.com/ can help you validate your JSON strings before you use them. – ADyson Jan 26 '23 at 09:36
  • And `count($mainArr)` won't work as you expect because $mainArr is a string, not a real array. The shortest route to solving this is probably to make the small adjustment I mentioned, to make it into valid JSON, then json_decode it into an array, and then you can loop through it without an issue. – ADyson Jan 26 '23 at 09:37
  • @ADyson i validate json and there was an error. After resolving Validation issue, my MainObject array still not updating with new object. – Najam Us Saqib Jan 26 '23 at 09:55
  • 1
    Why not just make a new array? And just add your new element on the loop you want it to be added? Or does it have to be used with array_splice? – Naruto Jan 26 '23 at 09:59
  • Your current code no longer causes the error you're still quoting. Please be clear about what output you're actually getting now. I agree, I don't know why you would use array_splice for this. – ADyson Jan 26 '23 at 10:12
  • current code outputs actual array. Not Updated array. – Najam Us Saqib Jan 26 '23 at 10:33
  • `current code outputs actual array`...probably because you wrote `print_r($mainArr);` instead of `print_r($decoded);`! But please see the answer below for a more logical solution. – ADyson Jan 26 '23 at 11:07
  • Related: [How to insert element into arrays at specific position?](https://stackoverflow.com/q/3353745/2943403) – mickmackusa Jan 27 '23 at 13:49

1 Answers1

1

It would probably make more sense to do this by creating a new array and adding items to it at the right time

e.g.

$mainArr = '[
    {"name":"Saqib1", "id":"1"},
    {"name":"Saqib2", "id":"2"},
    {"name":"Saqib3", "id":"3"},
    {"name":"Saqib4", "id":"4"},
    {"name":"Saqib5", "id":"5"},
    {"name":"Saqib6", "id":"6"},
    {"name":"Saqib7", "id":"7"},
    {"name":"Saqib8", "id":"8"},
    {"name":"Saqib9", "id":"9"},
    {"name":"Saqib10", "id":"10"}
]';


// Initial array of objects
$newObject = new stdClass(); // The new object you want to add

$newObject->name = 'New';
$newObject->id = '11';
$newObject->extraKey = 'this is extra key for newObjects to identify';

$n = 3; // The number of indexes after which to add the new object
$newArr = [];

$decoded = json_decode($mainArr);

for ($i = 0; $i < count($decoded); $i++) {
     $newArr[] = $decoded[$i];
     
    if (($i + 1) % $n == 0) {
        $newArr[] = $newObject;
    }
}

var_dump($newArr);

Demo: https://3v4l.org/ErQe7

ADyson
  • 57,178
  • 14
  • 51
  • 63