1
我使用slimphp v2和我有以下功能如何分割和改變JSON格式
function gt($user) {
$sql = "select id, id as categoryId, mobile, task from actions where status=0";
try {
$db = newDB($user);
$stmt = $db - > prepare($sql);
$stmt - > execute();
$gs = $stmt - > fetchAll(PDO::FETCH_OBJ);
if ($gs) {
$db = null;
echo json_encode($gs, JSON_UNESCAPED_UNICODE);
} else {
echo "Not Found";
}
} catch (PDOException $e) {
echo '{"error":{"text":'.$e - > getMessage().
'}}';
}
}
默認JSON輸出看起來像:
[{
"id": "1",
"categoryId": "1",
"mobile": "111",
"task": "test"
},
{
"id": "2",
"categoryId": "2",
"mobile": "222",
"task": "test2"
}]
和我試圖輸出製造。我需要生成一個ID:1_(id),然後像這樣格式化
[{
id: "1",
task: "test",
}, {
ID: "1_1", //generate this, add 1_id
categoryId: "1",
mobile: "00000",
}, {
id: "2",
task: "test2",
}, {
ID: "1_2", //generate this, add 1_id
categoryId: "2",
mobile: "11111"
}];
這可能嗎? 感謝
我上面的代碼顯示錯誤或? – Kitson88
它顯示一個錯誤500. http://pastebin.com/cmD4cqYB – UrL
如果你註釋掉我的代碼和var_dump($ gs);你有JSON輸出嗎?你可以把JSON放在你的問題中(編輯任何保密的東西) – Kitson88