I am using AngularJS with PHP on server side to access database. To make a POST method I write this request:
var req = {
method: 'POST',
url: 'action.php',
data:{'tblname': 'user',
'conditions' : {
'select' : 'user_name',
'where' : {
'user_category' : 'admin'
},
'order_by' : 'user_name'
}
};
In PHP I want to convert my JSON data object into a php associative array.
$request_data = json_decode(file_get_contents("php://input"));
$conditions = json_decode($request_data->conditions,true);
I used json_decode but it seems its not converting the JSON object to associative php array. I want the JSON object to be converted to the following PHP array:
$conditions = array(
"select" => "user_name",
"where" =>
array("user_category" => "admin") ,
"order_by" => "user_name"
);
$request_datacontains what you expect it to; you have already decoded the data, so it is no longer a string but an object. You should either decode the whole thing as an array or cast theconditionssection / object to an array.$conditions = (array) $request_data->conditions;$conditions = json_decode($request_data->conditions,true);with$conditions = (array) $request_data->conditions;and now it works. Thanks @jeroen and @splash58