我正在为Web服务器开发一个简单的API,您可以在其中为多个查询绑定到一个答案的查询设置答案。到目前为止,这是我的代码,但是我需要一种将查询(多键)附加到每个问题(值)的方法,而且我不确定如何将其组合在一起。$question = urldecode($_GET["q"]);$admin = urldecode($_GET["admin"]);$answer = urldecode($_GET["answer"]);$donext = urldecode($_GET["donext"]);if ($admin == "password123") { $file = fopen("program.json", "a+") or die ("file not found"); $json = file_get_contents('program.json'); $data = json_decode($json, true); $keys = array($q1, $q2, $q3); // need to append this with $q each time. $train = array_fill_keys($keys, '$a."+".$donext'); //$data[$tagid] = $tagvalue; $newjson = json_encode($data); file_put_contents('program.json', $newjson); fclose($file);} else { $file = fopen("program.json", "a+") or die ("file not found"); $json = file_get_contents('program.json'); $data = json_decode($json, true); $a = $data->$q; $piece = explode('+', $a); $reply = $piece[0]; $nextcontext = $piece[1]; fclose($file); echo $reply; echo $donext;}
1 回答
梦里花落0921
TA贡献1772条经验 获得超6个赞
如何将多个键设置为相同的值:
<?php
$questionkeys = array('hi','hey','yo');
$answervalue = "hello to you too";
$outputarray = array_fill_keys($questionkeys, $answervalue);
echo $outputarray;
?>
- 1 回答
- 0 关注
- 174 浏览
添加回答
举报
0/150
提交
取消