0

I am using this solution found on stackoverflow to encode my MYSQL output to a JSON encoded array.

$sth = mysql_query("SELECT ...");

$rows = array();

    while($r = mysql_fetch_assoc($sth)) {
        $rows[] = $r;
    }

print json_encode($rows);

This works great and produces an output of

[{"id":"81","title":"Something Here","start":"2009-10-27 09:00:00"},{"id":"77","title":"Report on water","start":"2009-10-30 09:00:00"}]

Now I need to put a value of say

"colour":"Blue"

within the JSON encoded array.

So i need the ouput to look like

[{"id":"81","title":"Community Awareness","start":"2009-10-27 09:00:00", "colour":"Blue"},{"id":"77","title":"Write a 10,000 Page Report on Emma","start":"2009-10-30 09:00:00", "colour":"Blue"}]

Does anyone have any solutions on how I could achieve this?

Thanks,

Tim Mohr

1 Answer 1

5

Before you call json_encode($rows), just edit the value in the $rows array:

$rows[0]['colour'] = 'Blue'; // changes the colour of the first row in the array

edit in fact, if you just want to add a colour to all of the rows, you can do a simple foreach:

foreach ($rows as &$row) {
    $row['colour'] = 'Blue';
}
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.