0

First of all I would just like to thank you for your help!

Now before I get into this I would like to state that I am good with PHP, but know NOTHING about javascript (I know this is an issue).

So I have a .json.php file for an API that I am using that takes in information in this format:

{
    "success": 1,
    "result": [
        {
            "id": "293",
            "title": "This is warning class event",
            "url": "http://www.example.com/",
            "class": "event-warning",
            "start": "1362938400000",
            "end":   "1363197686300"
        },
        {
            "id": "294",
            "title": "This is information class ",
            "url": "http://www.example.com/",
            "class": "event-info",
            "start": "1363111200000",
            "end":   "1363284086400"
        }
        ]
}

I am going to make a form that gets this information and puts it into a MySQL database, I know how to do this. Lets say I get the values from the MySQL database and they are stored like $data['id'], where id is the value in the above file. What I don't know is how to take those values and put it into this JSON PHP file above in the proper format. Is it possible and how do I do it?

3
  • You may want to look into json_encode: ca1.php.net/json_encode , it'll convert your array to that format Commented Feb 7, 2014 at 2:47
  • You have a json as input and want to process in php? Commented Feb 7, 2014 at 2:48
  • PHP as an input but format in a json file Commented Feb 7, 2014 at 2:56

2 Answers 2

2
$json = array('success' => 0,
              'result' => array());
while ($row = $stmt->fetch_assoc()) {
    $json['result'][] = $row;
}
$json['success'] = 1;
echo json_encode($json);
Sign up to request clarification or add additional context in comments.

4 Comments

Another great simple solution, However if you look in the format I need, there are [], How do I achieve this?
I have that. $json['result'] is an array, and each element is one row of the results.
So I can put this code right in the .json.php file?
Yes, along with the rest of the code that performs the database query.
1

Use json_encode

Let's say you have this:

$array = array(
    'key1' => 'value1',
    'key2' => 'value2',
);

Then you do:

$json = json_encode($array);
file_put_contents('path/to/file.json', $json);

And you have your Json-formatted object in your file.json file.

Note: you can use json_encode() with JSON_FORCE_OBJECT option to generate a Json object (without this option it generates an array, which is what you want, but other APIs may need objects). See PHP doc for more info.

5 Comments

Thorough response thank you very much, however if you look at the format I need, there are [], how do I achieve this?
wow sorry, actually you DON'T use JSON_FORCE_OBJECT to achieve this! Sorry for typing too fast :)
And the [] will add themselves when you have several elements in a row in one sub-array.
Maybe I am doing something wrong but this still does not work
Sorry I was formatting my array incorrectly! Sorry Sorry

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.