【问题标题】:Json array result in CodeIgniterCodeIgniter 中的 Json 数组结果
【发布时间】:2021-12-30 20:36:51
【问题描述】:

如何在使用 CodeIgniter 的控制器中获取这样的数据?

{
  "price": [
    [
      1483275269000,
      972.948
    ],
    [
      1483361668000,
      1025.88
    ]
  ]
}

我试过了:

$invoices = $this->invoice_model->getAllData2(logged('company_id'));
$response['price'] = $invoices;
echo json_encode($response,TRUE);

我只会这样:

{"price":[{"date_issued":"2021-03-01","grand_total":"972.948"},{"date_issued":"2021-03-12","grand_total":"1025.88"}]}

我该如何解决这个问题?

【问题讨论】:

  • 如果它有助于解决您的问题,请考虑接受答案。
  • 在此发帖的提示:(1)如果它有助于解决您的问题,请考虑接受答案; (2) 不求人; (3) 说出你的问题,让读者知道你仍然会承担大部分工作。

标签: php mysql json codeigniter


【解决方案1】:

您的数据只需要进一步处理即可以您喜欢的方式返回

$temp = [];
foreach($invoices as $key1 => $value1) {
    foreach($value1 as $key2 => $value2) {    
        switch($key2) {
            case 'date_issued':
                $temp[$key1][] = strtotime($value2);
            break;
            case 'grand_total':
                $temp[$key1][] = floatval($value2);
            break;
            default:
            break;
        }
    }
}
$response['price'] = $temp;
echo json_encode($response,TRUE);

【讨论】:

  • 这看起来不错,但$response 可以在这里进行一些初始化。
猜你喜欢
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
  • 2015-12-16
  • 2012-09-14
  • 1970-01-01
  • 2015-04-08
相关资源
最近更新 更多