user3386779
user3386779

Reputation: 7175

how to decode the encoded array value

I want to decode the array value in web service.I need to decode 'CONTENT_VALUES' field which has name and value as its object and pass it in json.ID and USER_ID return correct value(separate field) but name and question returns null. I want to decode
'ques' => $datas->name, 'answer' => $datas->value.value of CONTENT_VALUES=[{"name":"radio","value":"","sur_id":"3","user_id":"[email protected]","pagename":"question_response"},{"name":"true","value":""}]

$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);
}

Upvotes: 0

Views: 90

Answers (2)

Arun Krish
Arun Krish

Reputation: 2153

May be you should set use mb_convert_encoding before decoding like this

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

Upvotes: 1

1RV34
1RV34

Reputation: 79

Take a look at http://php.net/manual/en/function.json-decode.php.

This will describe how to convert a JSON string into a PHP object or array.

When you get stuck more with this you can fairly simple google "php decode json" and find plenty helpful resources.

Edit: Read the question too quick just before and understood it wrong, have you tried checking the return result of your $datas = json_decode($content) statement? Does CONTENT_VALUES actually contain JSON encoded data? Does the decoded object actually contain name and value?

Upvotes: 1

Related Questions