我有一个Json数组
{
"cms18.test.silverbee.nl": {
"domain": "cms18",
"template": "default"
},
"dmmd.test.silverbee.nl": {
"domain": "dmmd",
"template": "default"
},
"opmaat.test.silverbee.nl": {
"domain": "opmaat",
"template": "opmaat"
},
"opmaatdebiteurenadvies.nl": {
"domain": "opmaat",
"template": "opmaat"
},
"navbar.test.silverbee.nl": {
"domain": "navbar",
"template": "default"
},
"test18.test.silverbee.nl": {
"domain": "test18testsilverbeenl",
"template": "test"
},
"huisartsplus.test.silverbee.nl": {
"domain": "huisartsplustestsilverbeenl",
"template": "huisartsplus"
},
"robertenrademaker.test.silverbee.nl": {
"domain": "robertenrademakertestsilverbeenl",
"template": "robert-en-rademaker"
},
"tilburg.test.silverbee.nl": {
"domain": "tilburgtestsilverbeenl",
"template": "default"
},
"cbk-groningen.test.silverbee.nl": {
"domain": "cbk_groningentestsilverbeenl",
"template": "cbk-groningen"
},
"getbusyinc.test.silverbee.nl": {
"domain": "getbusyinctestsilverbeenl",
"template": "getbusyinc"
}
}
我得到了一个Php脚本,它获取输入值从上面将一个新元素推送到Json数组我不知道这是不是oke但是当我尝试推送它时我得到一个带有输入值的0它
<?php
$url = $_POST['new_url'];
$t_d = $_POST['t_d'];
$t_t = $_POST['t_t'];
$str = $url.":{"."domain:".$t_d.","."template:".$t_t."},";
if (isset($url))
{
array_push($list, $str);
}
?>
答案 0 :(得分:2)
如果它真的是一个json数组,你应该尝试将字符串解码为数组:
<?php
$decoded_list = json_decode($list, true);
?>
然后将新元素推入数组? 最后你可以做到
<?php
$list = json_encode($decoded_list);
?>
答案 1 :(得分:0)
$str = $url.":{"."domain:".$t_d.","."template:".$t_t."},";
$str = json_decode($str);
if (isset($url))
{
array_push($list, $str);
}
答案 2 :(得分:0)
首先,解码你的json:
$list = json_decode($list, true);
然后,根据您的用户输入值创建一个数组(下面的基本示例):
$url = "google.com";
$t_d = "google";
$t_t = "googletwo";
$data[] = $url;
$data[] = array('domain' => $t_d, 'template' => $t_t);
附加数据:
if(isset($url)){
array_push($temp, $data);
}
最后,将其编码回json,如下所示:
$json = json_encode($data);
输出:
[
"google.com",
{
"domain": "google",
"template": "googletwo"
}
]