如何解码编码的数组值

时间:2015-10-23 09:50:16

标签: php mysql arrays json

我想解码网络服务中的数组值。我需要解码' CONTENT_VALUES'将名称和值作为其对象并在json.IDUSER_ID中传递的字段返回正确的value(separate field),但名称和问题返回null。我想要解码为'ques' => $datas->name, 'answer' => $datas->value。值CONTENT_VALUES = [{" name":" radio"," value":&#34 ;"" sur_id":" 3"" USER_ID":" admin@gmail.com","页面名":" question_response"},{"名称":"真""值":"&#34 ;}]

$query1 = mysql_query("select * from `$prefix.response` where ID='$sur_id'");
while ($fetch = mysql_fetch_array($query1)) {
    $content = $fetch['CONTENT_VALUES'];
    $datas = json_decode($content);
    $test[] = array('ID' => $fetch['ID'], 'USERID' => $fetch['USER_ID'], 'ques' => $datas->name, 'answer' => $datas->value);
}

2 个答案:

答案 0 :(得分:1)

看看http://php.net/manual/en/function.json-decode.php

这将描述如何将JSON字符串转换为PHP对象或数组。

当你对此更加困难时,你可以相当简单的谷歌“php decode json”并找到大量有用的资源。

编辑:之前过快地阅读问题并理解错误,您是否尝试检查$datas = json_decode($content)声明的返回结果? CONTENT_VALUES实际上是否包含JSON编码数据?解码对象实际上是否包含namevalue

答案 1 :(得分:1)

可能是你应该在解码前设置使用mb_convert_encoding

$content = $fetch['CONTENT_VALUES'];
$content_value= mb_convert_encoding($content ,"UTF-8");
$datas = json_decode($content_value);