1

so I have an Array of JSON Objects generated from mysql query.

JSON Object looks like:

[
  {"field1": "value1", "field2": "value2"},
  {"field1": "value3", "field2": "value4"},
  {"field1": "value5", "field2": "value6"},
  {"field1": "value7", "field2": "value8"}
]

now I want to add empty object like this: {} after 3rd JSON object in the array.

expected result:

[
  {"field1": "value1", "field2": "value2"},
  {"field1": "value3", "field2": "value4"},
  {"field1": "value5", "field2": "value6"},
  {},
  {"field1": "value7", "field2": "value8"}
]

I am able to loop through each json object and I am able to replace the current object's value but not sure how to add a whole new json empty object.

My code

for($j = 0; $j < $length; $j++) {

            if($j == 1 ){
                // HERE I dont want to replace Jth JSON Obj but add new empty Obj, something like $data.push('{}')
                $data[$j]['field1'] = '';
                $data[$j]['field2'] = '';
            }else{
                $data[$j]['question'] = $ques_arr[$j];
                $data[$j]['response'] = $allresponses[$j]['response'];
            }
        }
3
  • try array_splice and new stdClass() Commented Jan 9, 2018 at 6:53
  • @Fei Can you show an example of what u mean by stdClass() ? Commented Jan 9, 2018 at 6:55
  • array_splice($data,$j,0,new stdClass()) this will insert an empty object at $j Commented Jan 9, 2018 at 7:04

2 Answers 2

3
    $json = '[
    {"field1": "value1", "field2": "value2"},
    {"field1": "value3", "field2": "value4"},
    {"field1": "value5", "field2": "value6"},
    {"field1": "value7", "field2": "value8"}
    ]';
    $temp = json_decode($json);
    $inserted = array( (object)[]);
    array_splice( $temp, 3, 0, $inserted );
    $result = json_encode($temp);
    var_dump($result);
Sign up to request clarification or add additional context in comments.

Comments

1

Try to add it via new \StdClass()OR [(object)[]] as example

$test = '[
{"field1": "value1", "field2": "value2"},
{"field1": "value3", "field2": "value4"},
{"field1": "value5", "field2": "value6"},
{"field1": "value7", "field2": "value8"}
]';
 $temp = json_decode($test);
 array_splice($temp, 3, 0, new \StdClass());
$result = json_encode($temp);
var_dump($result);

return [{"field1":"value1","field2":"value2"},{"field1":"value3","field2":"value4"},{},{"field1":"value7","field2":"value8"}]

update Use array_splice() and try :

    $test = '[
{"field1": "value1", "field2": "value2"},
{"field1": "value3", "field2": "value4"},
{"field1": "value5", "field2": "value6"},
{"field1": "value7", "field2": "value8"}
]';
    $temp = json_decode($test);
    $value = (new \StdClass());
    array_splice($temp, 3, 0, [(object)[]]);
    $result = json_encode($temp,true);
    var_dump($result);

as you wanted.

2 Comments

You have replaced the third Json Object to empty {}. I dont want to replace it but add a new object
@MurlidharFichadia I resolved it, check my answer again plz.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.