Append a key/Value to existing array

Hi,
I have the following output json:

[
{
“user_id”: 999,
“user_uuid”: “xxxxxx”,
“first_name”: “xxxxx”,
“last_name”: “xxxxx”,
“gender”: null,
“email_address”: “[email protected]”,
“country”: “XX”,
“date_of_birth”: null,
“stripe_id”: “xxxxxxxxxx”
}
]

I am getting t using below PHP:

$jsonData = array();

while($mysql_row = $mysql_query->fetch())
{
    $jsonData[] = $mysql_row;
}

echo json_encode($jsonData);

How can I append the following:

"authenticated": "true",

so the final output will be like this:

[
{
“user_id”: 999,
“user_uuid”: “xxxxxx”,
“first_name”: “xxxxx”,
“last_name”: “xxxxx”,
“gender”: null,
“email_address”: “[email protected]”,
“country”: “XX”,
“date_of_birth”: null,
“stripe_id”: “xxxxxxxxxx”,
“authenticated”: “true”
}
]

Well, if you create a standard array like =array(), then you are not using Json…
And, so, you can just add it like you did, but, sort of… More like $jsonData[$key] = $data;
But, if it was a Json Object, you would use push like this: array_push($jsonData, $data);
Which will add it to the end of the array. Hope that helps…

PHP has a ton of functions for arrays
https://www.php.net/manual/en/function.array-merge.php
https://www.php.net/manual/en/function.array-combine.php

Sponsor our Newsletter | Privacy Policy | Terms of Service