[英]Convert JSON object to associative php array
I am using AngularJS with PHP on server side to access database.我在服务器端使用带有 PHP 的 AngularJS 来访问数据库。 To make a POST method I write this request:为了制作一个 POST 方法,我写了这个请求:
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.在 PHP 中,我想将我的 JSON data
对象转换为 php 关联数组。
$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.我使用了json_decode
但它似乎没有将 JSON 对象转换为关联的 php 数组。 I want the JSON object to be converted to the following PHP array:我希望将 JSON 对象转换为以下 PHP 数组:
$conditions = array(
"select" => "user_name",
"where" =>
array("user_category" => "admin") ,
"order_by" => "user_name"
);
You are trying to json_decode
data that has already been decoded.您正在尝试对已解码的数据进行json_decode
。
Once you do:一旦你这样做:
$request_data = json_decode(file_get_contents("php://input"), TRUE);
You've already have the information in an associative array.您已经拥有关联数组中的信息。 (The second parameter tells json_decode()
that you want your result as an associative array and not as an object ). (第二个参数告诉json_decode()
您希望结果作为关联数组而不是对象)。
The next step is as simple as:下一步很简单:
$conditions = $request_data['conditions'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.