使用php将整数转换数据库转换为json

时间:2016-09-16 08:32:32

标签: php arrays json

$list = array();
$sql="SELECT * FROM app_maths WHERE student_id=1043
UNION
SELECT * FROM comm_skills WHERE student_id=1043
UNION
SELECT * FROM eng_draw WHERE student_id=1043;";
$query = mysqli_query($conn,$sql);

while ($row = mysqli_fetch_array($query)) {
$sub="subject";
$sem=intval('sem');

array_push($list,array("subject"=>$row[$sub],"sem"=>$row[$sem],"s1"=>$row['s1'],"s2"=>$row['s2']));

//  echo $sem;
}
echo json_encode(array('result' => $list ))."<hr>";

数据库的每一行都有 sem = 1 。我可以轻松地将此信息转换为json字符串,但我想将json转换为int而不是字符串以进行进一步计算。使用intval()为我提供了值&#39; sem&#39; =&#39; 1043&#39; ,这也是JSON中的字符串。

1 个答案:

答案 0 :(得分:0)

$sem=intval('sem');对我来说似乎不对。

尝试删除该行,并在推送行中使用intval($row['sem']),这会将实际的数组项值转换为int。