将JSON对象转换为关联的php数组

时间:2018-11-02 09:44:09

标签: php json

我在服务器端使用AngularJS和PHP来访问数据库。要编写POST方法,请编写以下请求:

var req = {
      method: 'POST',
      url: 'action.php',
      data:{'tblname': 'user',
      'conditions' : {
            'select' : 'user_name',
            'where' : {
                 'user_category' : 'admin'
             },
       'order_by' : 'user_name'
       }   
};

在PHP中,我想将JSON data对象转换为php关联数组。

$request_data = json_decode(file_get_contents("php://input"));
$conditions = json_decode($request_data->conditions,true);

我使用了json_decode,但它似乎没有将JSON对象转换为关联的php数组。我希望将JSON对象转换为以下PHP数组:

$conditions = array(
        "select" => "user_name",
        "where" =>
            array("user_category" => "admin") ,
        "order_by" => "user_name"
);

1 个答案:

答案 0 :(得分:1)

您正在尝试json_decode已解码的数据。

一旦您这样做:

$request_data = json_decode(file_get_contents("php://input"), TRUE);

您已经将信息包含在关联数组中。 (第二个参数告诉json_decode() that you want your result as an associative array and not as an object)。

下一步很简单:

$conditions = $request_data['conditions'];
相关问题