I have problems with converting the result of a database query to a json array. My Output looks like this:
{
"user_id": "1",
"username": "testuser1",
"user_permissions": [
{
"list_id": "1"
},
{
"list_id": "2"
},
{
"list_id": "3"
}
],
"android_app_id": null,
"iat": 1537694955,
"exp": 1537702155
}
The brackets ({}) around the JSON array lead to problems parsing the array in the client's response. I just need to have a simple array like (1, 2, 3).
The Array is produced by database query result (PHP) and then using SLIM3 Operation $this->response->withJson
:
$query = "SELECT list_id FROM permissions WHERE user_id='$user_id'";
$sth = $this->dbconnection->prepare($query);
$sth->execute();
$result_permissions = $sth->fetchAll();
return $result_permissions;
I really have problems to convert database results to normal JSON arrays, because PHP only knows associative arrays (numeric or with keys) which leads to bad formatted json arrays.
The json Output is returned to the server. Using SLIM3 Framework I access the JSON Data and the permissions array like this:
$user_permissions = $decoded_response['user_permissions'];
Now i try to get list-ids with $user_permissions[list'id][0]
which gives 1 using print_r
command.
What I want to do next is using a database query with IN Operator to check for the permission_ids. Therefore I need to produce an array like (1, 2, 3).. I'm stuck right now, because I don't know how to produce such array from the JSON..
For me the easiest approach would be to directly produce such an array after the database query and add it to the JSON at the beginning but I don't know how to achieve that.
Any hints?