0

I am returning json response to api, using json_encode() method. I have array which is where a key users can have multiple user or no user i.e empty array,and key course is either empty or can have single object as below

{
"success": true,
"code": 200,
"message": "",
"result": {
    "users": [],
    "course": []
}

}

if it has data the response is like this

{
  "success": true,
  "code": 200,
  "message": "",
  "result": {
    "users": [
        {
            "name": "Vishal",
            "Age": 25
        },
        {
            "name": "Akshay",
            "Age": 29
        }
    ],
    "course": {
        "name": "xyz"
    }
  }
 }

but what i want is users should be array in both cases and course should return as object in both cases. is it possible.??

1 Answer 1

2

You can force course entry to be object before json encoding.

 $ret["result"]["course"] = (object)$course;
Sign up to request clarification or add additional context in comments.

Comments

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.