[英]Convert JSON object to associative php array
我在服務器端使用帶有 PHP 的 AngularJS 來訪問數據庫。 為了制作一個 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"
);
您正在嘗試對已解碼的數據進行json_decode
。
一旦你這樣做:
$request_data = json_decode(file_get_contents("php://input"), TRUE);
您已經擁有關聯數組中的信息。 (第二個參數告訴json_decode()
您希望結果作為關聯數組而不是對象)。
下一步很簡單:
$conditions = $request_data['conditions'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.