1

I'm just getting into php and I have an issue with the return of a JSON object.

Here's the code:

$stmt->bind_result($image_link, $start_time, $end_time, $viet_performer, $english_performer, $viet_event, $english_event, $day, $stage);

            while($stmt->fetch())
            {
                echo json_encode([["image_link" => $image_link,"start_time" => $start_time, "end_time" => $end_time, "viet_performer" => $viet_performer,
                                "english_performer" => $english_performer, "viet_event" => $viet_event, "english_event" => $english_event, "day" => $day,
                                "stage" => $stage]]);

                $stmt->bind_result($image_link, $start_time, $end_time, $viet_performer, $english_performer, $viet_event, $english_event, $day, $stage);
            }

This basically outputs:

{"image_link":"schedule_music.jpg","start_time":"17:00","end_time":"18:30","viet_performer":"","english_performer":"","viet_event":"","english_event":"Tet Music","day":0,"stage":1} {"image_link":"schedule_music.jpg","start_time":"11:00","end_time":"12:00","viet_performer":"","english_performer":"","viet_event":"Nh?c","english_event":"Music","day":1,"stage":0}

//repeat again

However, according to a JSON validator, it's an invalid JSON object.

I would like to produce a valid JSON like so:

[{"image_link":"schedule_music.jpg","start_time":"17:00","end_time":"18:30","viet_performer":"","english_performer":"","viet_event":"","english_event":"Tet Music","day":0,"stage":1},{"image_link":"schedule_music.jpg","start_time":"11:00","end_time":"12:00","viet_performer":"","english_performer":"","viet_event":"Nh?c","english_event":"Music","day":1,"stage":0}]

Notice the start/end brackets as well as a comma separator.

How can I achieve this?

Here is a better example (found somewhere) of how my output similarly looks like:

{
    "id": "a1",
    "session": "General",
    "name": "Exhibitor Setup Begins",
    "startTime": "0900",
    "details": "9am Exhibitor Hall",
    "png": "image",
    "speaker1": "Johnson",
    "speaker2": "Nelson",
    "speaker3": ""
}{
    "id": "b1",
    "session": "General",
    "name": "Conference Registration",
    "startTime": "1000",
    "details": "10am Noon Upper Level Lobby",
    "png": "image",
    "speaker1": "Jackson",
    "speaker2": "",
    "speaker3": ""
}

And how I would like to have the output look like:

[
    {
        "id": "a1",
        "session": "General",
        "name": "Exhibitor Setup Begins",
        "startTime": "0900",
        "details": "9am Exhibitor Hall",
        "png": "image",
        "speaker1": "Johnson",
        "speaker2": "Nelson",
        "speaker3": ""
    },
    {
        "id": "b1",
        "session": "General",
        "name": "Conference Registration",
        "startTime": "1000",
        "details": "10am Noon Upper Level Lobby",
        "png": "image",
        "speaker1": "Jackson",
        "speaker2": "",
        "speaker3": ""
    }
]
0

2 Answers 2

5

Regarding your updated code, the problem is that you are encoding each piece separately rather than encoding all of the data at once. Try this:

$stmt->bind_result($image_link, $start_time, $end_time, $viet_performer, $english_performer, $viet_event,
    $english_event, $day, $stage);

$data = [];
while ($stmt->fetch()) {
    $data[] = [
        "image_link"        => $image_link,
        "start_time"        => $start_time,
        "end_time"          => $end_time,
        "viet_performer"    => $viet_performer,
        "english_performer" => $english_performer,
        "viet_event"        => $viet_event,
        "english_event"     => $english_event,
        "day"               => $day,
        "stage"             => $stage
    ];
}

echo json_encode($data);

Response based on earlier version of the question:

It looks like you want an array of objects:

echo json_encode([
    [
        "image_link"        => $image_link,
        "start_time"        => $start_time,
        "end_time"          => $end_time,
        "viet_performer"    => $viet_performer,
        "english_performer" => $english_performer,
        "viet_event"        => $viet_event,
        "english_event"     => $english_event,
        "day"               => $day,
        "stage"             => $stage
    ]
]);

Note that an array containing string keys is going to be converted to an object by json_encode. In the above code, the inner array becomes an object while the outer array does not.


This may be a more intuitive way to look at it:

$objectOne = (object) [
    "image_link"        => $image_link,
    "start_time"        => $start_time,
    "end_time"          => $end_time,
    "viet_performer"    => $viet_performer,
    "english_performer" => $english_performer,
    "viet_event"        => $viet_event,
    "english_event"     => $english_event,
    "day"               => $day,
    "stage"             => $stage
];

$objectTwo = (object) [
    "image_link"        => $image_link,
    "start_time"        => $start_time,
    "end_time"          => $end_time,
    "viet_performer"    => $viet_performer,
    "english_performer" => $english_performer,
    "viet_event"        => $viet_event,
    "english_event"     => $english_event,
    "day"               => $day,
    "stage"             => $stage
];

echo json_encode([$objectOne, $objectTwo]);
Sign up to request clarification or add additional context in comments.

8 Comments

This does not produce the result I need. This will wrap every json object into one array, like so [{"image_link":"schedule_huong_sy_nhan.jpg","start_time":"13:30","end_time":"14:00","viet_performer":"","english_performer":"Huong Sy Nhan","viet_event":"","english_event":"Singing","day":2,"stage":0}][{"image_link":"schedule_unknown.png","start_time":"21:00","end_time":"","viet_performer":"","english_performer":"","viet_event":"","english_event":"End of Festival","day":2,"stage":1}]. Notice the brackets. However, I would like it to be like in the expected output....unless I'm not understanding.
If it helps, I would like it to be as shown in the solution here: stackoverflow.com/questions/22326273/…
@Pangu Unless I'm missing something, I still think this is the solution you're looking for. I added an additional example to clarify.
this is what I added: echo json_encode([["image_link" => $image_link,"start_time" => $start_time, "end_time" => $end_time, "viet_performer" => $viet_performer,"english_performer" => $english_performer, "viet_event" => $viet_event, "english_event" => $english_event, "day" => $day,"stage" => $stage]]); However, this does not produce the same output as the clearer example I provided.
@Pangu The output you expect contains two objects. You are only passing one. See my second example.
|
0

The correct way is to generate the data structure first, then encode it:

$foo = [];
while($stmt->fetch()) {
    $foo []= [
         "image_link" => $image_link,
         "start_time" => $start_time,
         "end_time" => $end_time,
         "viet_performer" => $viet_performer,
         "english_performer" => $english_performer,
         "viet_event" => $viet_event,
         "english_event" => $english_event,
         "day" => $day,
         "stage" => $stage];
    ];
}
$json = json_encode($foo);

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.