-2

I'm inserting value with implode and get this type json

[  
   {  
      "id":"1",
      "f_name":"Mitrajit",
      "l_name":"Samanta",
      "class":"XII",
      "section":"A,B,C",
      "roll":"1",
      "status":"1"
   }
]

but I want this type :-

[
  {
    "f_name" : "Mitrajit",
    "l_name" : "Samanta",
    "class"  : "XII",
    "section": ["A","B","C"],
    "roll"   : "1",
    "status" : "1"
  }
]

how can I get "A,B,C" to ["A","B","C"]?

Mohammad
  • 21,175
  • 15
  • 55
  • 84

1 Answers1

1

You need to use json_decode() to converting json to php array. Then select section value in json array and use explode() to converting it to array. At the end convert php array to json using json_encode()

$json = json_decode($jsonStr, true);
$json[0]["section"] = explode(",", $json[0]["section"]);
$jsonStr = json_encode($json);

Check result in demo

Mohammad
  • 21,175
  • 15
  • 55
  • 84