本質上,我有以下陣列,其中包含通過函式傳遞的各種值。然后需要將每個輸出組合成一個 JSON 陣列,如下所示:
"response": {
"firstvalue": 4,
"secondvalue": 1,
"thirdvalue": "String Response 1",
"fourthvalue": "String Response 2"
}
到目前為止的代碼:
<?php
header('Content-Type: application/json');
$arrayvalues =
["34jkw9k2k9w",
"k4otk320el01oeoo20",
"30f0w2l020wk3pld==",
"3c2x3123m4k43=="];
foreach($arrayvalues as $item) {
$decrypted = myFunction($item, $action = 'decrypt');
$response["firstvalue"] = $decrypted;
$response["secondvalue"] = $decrypted;
$response["thirdvalue"] = $decrypted;
echo json_encode($response);
}
?>
如何才能做到這一點?
uj5u.com熱心網友回復:
header('Content-Type: application/json');
$arrayvalues =
["34jkw9k2k9w",
"k4otk320el01oeoo20",
"30f0w2l020wk3pld==",
"3c2x3123m4k43=="];
// Is this necessary?
$keys = ['firstvalue', 'secondvalue', 'thirdvalue', 'fourthvalue'];
$result = [];
foreach($arrayvalues as $idx => $item) {
$result[$keys[$idx]] = myFunction($item, $action = 'decrypt');
}
echo json_encode(['response' => $result], JSON_PRETTY_PRINT);
UPD:添加response嵌套。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/394288.html
上一篇:以更有效的方式更新Json
