0

I have a JSON which I used to create html form, now I want to submit values and replace json values responding to their category and save them elsewhere.

Here is the JSON:

    "subject":"Birth place verification",
    "preface" : "To Whom it may concern । ",
    "content" :[
        {
            "type":"label",
            "value":"previous VDC"
        },
        {
            "type":"text",
            "value_category":"vdc_municipality_name",
            "value":"",
            "width":"300px",
            "input_type":"text"
        },
        {
            "type":"label",
            "value":"ward number"
        },
        {
            "type":"text",
            "value_category":"ward_number",
            "value":"",
            "width":"80px",
            "input_type":"number"
        }]
} 

Now here is what is tried:

 $formData = json_decode(File::get(public_path('english-form.json')), true);
    $form_content = $formData['content'];
    foreach($form_content as $key => $field ){
        
        if ($field['type'] == 'text'){
          $value_category = $field['value_category'];
          $field['value'] = $request->$value_category;
        //   return $field;
        }
       
    }
    $updatedJsonData = json_encode($formData, JSON_PRETTY_PRINT);
    return $updatedJsonData;

I tried to get request name, from json then assign value to respective field. while returning field, the value is changed, but outside for each updatedJsonData has no change.

  • 1
    This is because the foreach uses call by value. Either replace `$key => $field` with `$key => &$field` or use `$form_content[$key]['value']` – Sachin Bahukhandi Jun 05 '23 at 07:13

0 Answers0