【发布时间】:2015-02-11 06:26:07
【问题描述】:
在我的查询中,我有 3 个 tinyint 字段,通过获取它们会给出 0/1 值。但是在获取所有数据后,我想将该结果编码为 JSON 格式,该格式应该得到 true/false 而不是 0/1。 在我的代码中,我已经完成了类型转换,这给出了正确的结果。但我应该为所有我不想要的 tinyint 字段写。 有什么方法可以检查 tinyint 字段并进行转换。
PHP CODE:
$result= mysql_query("select incident_main.inc_id as incident_id,
ward_master.ward_id as 'ward_id',
GROUP_CONCAT(incident_log.inclog_date) as revisit_dates ,
inc_GISlat as lat,
inc_GISlon as lon,
inc_closed as b_is_closed,
inc_closeDate as closed_date,
incident_statuscd.inc_status_desc as status,
inc_date as 'incident_date',
inc_patientName as 'patient_name',
inc_patientAge as 'patient_age',
inc_patientGender as 'patient_gender',
inc_patientMobile as'patient_mobile' ,
inc_patientAddress as 'patient_address',
inc_type as type,
inc_typeOther as 'type_other',
inc_diagnosis as diagnosis,
inc_recurrence as recurrence,
inc_hospitalized as b_hospitalized,
inc_treatment as treatment ,
inc_treatmentOther as 'treatment_other'
from
incident_main,
incident_statuscd ,
ward_master,
incident_log
where
inc_status=incident_statuscd.inc_status_id
and
incident_main.inc_patientWard=ward_master.ward_id
and incident_log.inc_id=incident_main.inc_id ")
or die(mysql_error);
while($res = mysql_fetch_assoc($result)) {
foreach($res as $key => $value) {
if(substr( $key, 0, 2 ) === "b_") {
// assign new value
$res[substr($key,2,strlen($key))] = !!$value;
//clean up old value
unset($res[$key]);
};
}
$data=array_filter($res);
echo json_encode($data, true);
请帮忙!! 提前致谢。
【问题讨论】:
-
JS 也有类型转换。因此,如果您不想这样做,请不要这样做。
-
您能否更具体地解释一下......在哪里以及如何?