如何从我的PHP数组(mysql_fetch_array)转换为声明的PHP类,然后将其编码为json格式字符串。我的阵列:
while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)){
$patient[] = array( 'id' => $rows['id'],
'name' => $rows['name'],
'sex' => $rows['sex'],
'civil_status' => $rows['civil_status'],
'age' => $rows['age'],
'type_of_admission' => $rows['type_of_admission'],
'admission_diagnosis' => $rows['admission_diagnosis'],
'date_admitted' => $rows['date_admitted']);
}
我的声明PHP类
class Person
{
public $id;
public $name;
public $sex;
public $civil_status;
public $age;
public $type_of_admission;
public $admission_diagnosis;
public $date_admitted;
}
答案 0 :(得分:1)
我认为您正在寻找mysql_fetch_object
:
while ($row = mysql_fetch_object($result, 'Person')){
$json_rows[] = json_encode($row);
}
此外,您应该使用mysqli,same basic concept。
答案 1 :(得分:0)