我正在尝试对行选项进行JSON解码,并使用其余数据对其进行编码。但这只是给我“选项”行而已。如果我不解码,请在“选项”行中给我加上反斜杠。
[{“ ID”:“ 4”,“ AppID”:“ 1”,“问题”:“ test2”,“类型”:“收音机”,“订单号”:“ 2”,“选项”:“ {\“1号\”: \“是\”,\“ Number2 \”:\“否\”}“}]
//open connection to mysql db
$connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbdata) or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select * from App_Questions";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray = array();
while ($row =mysqli_fetch_assoc($result)) {
if ($row["Options"]){
$str = json_decode($row["Options"]);
$emparray = $str;
}else{
$emparray[] = $row;
}
}
echo json_encode($emparray);
答案 0 :(得分:0)
假设您要对保存有json数据的“选项”字段进行解码,然后要使用其余的其他字段对这些json数据进行编码。 json解码的输出是来自我们的问题:
array (
0 =>
array (
'ID' => '4',
'AppID' => '1',
'Question' => 'test2',
'Type' => 'Radios',
'OrderNumber' => '2',
'Options' => '{"Number1": "Yes", "Number2": "No"}',
),
)
如果解码“选项”,则会得到以下信息:
array (
'Number1' => 'Yes',
'Number2' => 'No',
)
您可以执行以下操作,以对循环中的其余数据进行编码:
$decodedData = json_decode($results);
foreach ($decodedData as $row) {
if (isset($row->Options)) {
$decodedOptions = json_decode($row->Options);
foreach ($decodedOptions as $key => $value) {
$row->$key = $value;
}
}
}
$decodedData = json_encode($decodedData);
print_r($decodedData);