【发布时间】:2023-04-04 06:40:02
【问题描述】:
基本上我有以下数组,其中包含通过函数传递的各种值。然后需要将每个的输出组装成一个 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);
}
?>
如何做到这一点?
【问题讨论】:
-
总是只有四个值吗?
-
总是有预设数量的值,是的。 4 只是一个例子。
-
那个答案有点不清楚。你的意思是总是有相同数量的值,值的数量不会改变?还是您的解决方案需要灵活?
-
是的,总会有相同数量的值,很抱歉造成混淆。