eggplantSword's avatar

Convert php array with single object to json object

I had an array with many objects inside but I removed them all but one and now I have an array with a singular object inside. I need it to be a object and not an array because it was originally an object and in order to do some sorting and filtering I changed it to an array but now I need it to be an object again.

I tried this without success

 $f = json_decode(json_encode($f));

A dd on $f returns this, as you can see it's one object in an array I would like it to be just the object not inside an array.

[
   {
      "id":1,
      "question":"adasdf",
      "instruction":null,
      "user_id":1,
      "survey_section_id":1,
      "response_type_id":3,
      "optional":0,
      "num":null,
      "rank":4,
      "show_text":0,
      "image":null,
      "video":null,
      "created_at":"2020-06-19 08:54:27",
      "updated_at":"2020-06-19 08:54:27",
      "pivot":{},
      "answer":[],
      "answer_pager":[]
   }
]

How can I fix this?

0 likes
4 replies
frankielee's avatar

You want it back to the model object?

$model = new Model($f[0]);

eggplantSword's avatar

@frankielee If I do that it returns this which removes almost everything that I need

{"question":"adasdf","survey_section_id":1,"response_type_id":3,"optional":0,"num":null,"rank":4,"image":null}
frankielee's avatar

why are you trying to get all the objects and removed them afterward?

Please or to participate in this conversation.