将PHP数组转换或放置到类变量

时间:2013-09-20 01:24:32

标签: php arrays class

如何从我的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;
  }

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找mysql_fetch_object

while ($row = mysql_fetch_object($result, 'Person')){

    $json_rows[] = json_encode($row);

}

此外,您应该使用mysqlisame basic concept

答案 1 :(得分:0)

只需重新输入(object)

即可
$object = (object) $array_name;

代表json_encode