【问题标题】:PHP to JSON encode with associative arrayPHP 到 JSON 编码与关联数组
【发布时间】:2015-10-27 18:09:48
【问题描述】:
require "constant.php";
require "database.php";
$sql = "select e.*, b.booth_address, ba.bank_full_name from $app_booth_enrolment e
left join $app_booth_info b on e.booth_id=b.booth_id
left join $app_bank_info ba on e.bank_id=ba.bank_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
    $data = array();
    while($row = $result->fetch_assoc()){
        array_push($data,$row);
    }
    echo $data = json_encode($data);  
    var_dump(json_encode($data));
}else{
    $data['status'] = "No terminal found";
    echo $data = json_encode($data);
}

echo json_encode($data); 只打印 print_r($data) 打印一个数组。如何将数据数组转成json格式。

Array
(
    [0] => Array
        (
            [id] => 1
            [enrolment_id] => 101-1-0001
            [terminal_type_id] => ATM
            [booth_name] => CITY-Bashundhara
            [enrolment_date] => 2015-01-01
            [live_date] => 2015-02-02
            [bank_id] => 101
            [booth_id] => 101-0001
            [terminal_serial] => 5300153835
            [terminal_host] => TCBL-TWO
            [terminal_app] => 
            [note] => 
            [status] => 1
            [insert_by] => 1
            [insert_date] => 
            [booth_address] => Head Office, Jiban Bima Tower, 10, Dilkusha Commercial Area, Dhaka-1000 
            [bank_full_name] => The City Bank Limited
        )

    [1] => Array
        (
            [id] => 2
            [enrolment_id] => 101-1-0002
            [terminal_type_id] => ATM
            [booth_name] => CITY-VIP_Road
            [enrolment_date] => 2015-01-01
            [live_date] => 2015-02-02
            [bank_id] => 101
            [booth_id] => 101-0002
            [terminal_serial] => 5300153831
            [terminal_host] => TCBL-TWO
            [terminal_app] => 
            [note] => 
            [status] => 1
            [insert_by] => 1
            [insert_date] => 
            [booth_address] => Head Office, Jiban Bima Tower, 10, Dilkusha Commercial Area, Dhaka-1000 
            [bank_full_name] => The City Bank Limited
        )

    [2] => Array
        (
            [id] => 3
            [enrolment_id] => 101-2-0003
            [terminal_type_id] => CDM
            [booth_name] => CITY-DSE
            [enrolment_date] => 2015-01-01
            [live_date] => 2015-02-02
            [bank_id] => 101
            [booth_id] => 101-0003
            [terminal_serial] => 5300153832
            [terminal_host] => TCBL-TWO
            [terminal_app] => 
            [note] => 
            [status] => 1
            [insert_by] => 1
            [insert_date] => 
            [booth_address] => Head Office, Jiban Bima Tower, 10, Dilkusha Commercial Area, Dhaka-1000 
            [bank_full_name] => The City Bank Limited
        ) 
)

【问题讨论】:

  • 尝试使用yii2 encode()函数:yiiframework.com/doc-2.0/…
  • 你能告诉我们print_r($data) 中有什么吗?
  • var_dump(json_encode($data)); 究竟返回了什么
  • var_dump(json_encode($data));返回布尔(假)
  • 如果是这种情况,您应该致电json_last_error_msg() 看看出了什么问题

标签: php json associative-array


【解决方案1】:

好吧,我找到了解决方案。

Havelock 建议打印 json_last_error_msg(),我发现格式错误的 UTF-8 字符,可能编码不正确。

我正在使用MySQLi,所以我必须在数据库连接后将此行放入以进行字符格式化:mysqli_set_charset($conn, "utf8");

谢谢哈夫洛克。

【讨论】:

    猜你喜欢
    • 2011-03-05
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多