[英]Why doesn't my php function return a json formatted value?
I am trying to store a couple of form fields in a mysql database. 我正在尝试将几个表单字段存储在mysql数据库中。 These specific fields can have multi selected values. 这些特定字段可以具有多个选择的值。 So i like to store the value as json_encode formatted value. 所以我喜欢将值存储为json_encode格式的值。
Now when I code per form field i can store the values in json format in the mysql db. 现在,当我为每个表单字段编码时,我可以将json格式的值存储在mysql数据库中。 Because of the repetitions i tried this function but this returns an array 由于重复,我尝试了此函数,但返回了一个数组
function radioValue($radiodata) {
$tmpArray = array();
$tmpArrayLen = count($radiodata);
for ($i = 0; $i < $tmpArrayLen; $i++) {
$tmpArray[$i] = $radiodata[$i];
}
$tmpValue = json_encode($tmpArray);
return $tmpValue;
}
So not {"1":"value1"} but ["value1"] 因此,不是{“ 1”:“ value1”},而是[“ value1”]
What have I overlooked?? 我忽略了什么?
btw this is the part why worked for each field 顺便说一下,这就是为什么要为每个领域工作的部分
$tmpArray = array();
$len = count($posted_data["field1"]);
for ($i = 0; $i < $len; $i++) {
$tmpArray[$i] = $posted_data["field1"][$i];
}
$storeValue = json_encode($tmpArray);
You have to decode it after you encode: 编码后必须对其进行解码:
$futureArray = radioValue($radiodata);
$array = json_decode($futureArray);
Also, add a true
as the second parameter and it will be a associative array 另外,添加true
作为第二个参数,它将是一个关联数组
$array = json_decode($futureArray, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.