Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Return JSON from php script

Hi,

I have a PHP script. I want to output a result as a JSON string/object. I'd like the output to look like this:

{
  "message": {
    "text": "Students fetched ok!",
  },
  "students": [
    {
      "student_id": "123",
      "student_name": "john doe"
    },
    {
      "student_id": "456",
      "student_name": "jane doe"
    }
  ]
}

not sure where to get started though. Looks like this is one big JSON object, which has a 'message' object, then an array of students (which I'll fetch from a database). What's a standard way people generate these types of responses from php scripts?

Thanks
Avatar of nergik
nergik
Flag of Spain image

hello

in php encoding json objects its really easy, you only need to:

echo json_encode($yourdata);

and php will convert your array in json format

for more info: php.net/json_encode
ASKER CERTIFIED SOLUTION
Avatar of LordZardeck
LordZardeck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial