【问题标题】:Convert MySQL join to JSON array将 MySQL 连接转换为 JSON 数组
【发布时间】:2018-05-30 15:51:36
【问题描述】:

我有一个名为calls 的表和一个名为uploads 的表,其结构如下:

来电

╔═════════╦═══════════╦═════════════════════╗
║ call_id ║ caller_id ║ call_time           ║
╠═════════╬═══════════╬═════════════════════╣
║ 235     ║ 23        ║ 2017-11-01 12:47:27 ║
╠═════════╬═══════════╬═════════════════════╣
║ 259     ║ 65        ║ 2017-11-02 16:58:27 ║
╚═════════╩═══════════╩═════════════════════╝

上传

╔═══════════╦═════════╦═════════════════════╗
║ upload_id ║ call_id ║ file_name           ║
╠═══════════╬═════════╬═════════════════════╣
║ 145       ║ 235     ║ bu2t7384uhjnfns.mp3 ║
╠═══════════╬═════════╬═════════════════════╣
║ 146       ║ 235     ║ jbwer8y23gr92o.mp3  ║
╚═══════════╩═════════╩═════════════════════╝

然后我有一个连接两个表的查询:

SELECT calls.*, uploads.*
FROM cads
LEFT OUTER JOIN uploads ON uploads.call_id = 235

当我在 codeigniter 中使用 return $query->result_array() 然后 json_encode 它返回一个包含两个 call 元素的数组:

calls:(2) [{…}, {…}] 

我希望我的 JSON 对象中只有一个 call 元素,但有一个名为 uploads 的键,它是上传表中的上传数组,例如像这样:

calls: Array(1)
  0:
    call_id: 235
    caller_id: 23
    call_time: 2017-11-01 12:47:27
    uploads: Array(2)
       0: 
         upload_id: 145
         call_id: 235
         file_name: bu2t7384uhjnfns.mp3
       1:
         upload_id: 146
         call_id: 235
         file_name: jbwer8y23gr92o.mp3

任何可以向我展示如何在 Handlebars-JS 中显示上述 JSON 的人的奖励积分 :)

【问题讨论】:

  • calls:(2) [{…}, {…}] 中有什么内容? (最好以数组或 json 格式显示)。您对结果满意还是希望我们也重写它?你试过什么? Handlebars 用它做什么,json 是 json..

标签: php mysql json codeigniter


【解决方案1】:

假设$results是从数据库返回的,看起来像这样

array (size=2)
  0 => array (size=5)
      'call_id' => string '235' (length=3)
      'caller_id' => string '23' (length=2)
      'call_time' => string '2017-11-01 12:47:27' (length=19)
      'upload_id' => string '145' (length=3)
      'file_name' => string 'bu2t7384uhjnfns.mp3' (length=19)
  1 => array (size=5)
      'call_id' => string '235' (length=3)
      'caller_id' => string '23' (length=2)
      'call_time' => string '2017-11-01 12:47:27' (length=19)
      'upload_id' => string '146' (length=3)
      'file_name' => string 'jbwer8y23gr92o.mp3 ' (length=19)

这是按照您的要求重新格式化两个数据库行的一种方法。

$calls = $results[0];
//remove unwanted indexes
unset($calls['upload_id'], $calls['upload_id'], $calls['file_name']);
//gather the upload sub-arrays
foreach($results as $result)
{
     //remove unwanted indexes
     unset($result['caller_id'], $result['call_time']);
     //add sub-array to "upload" 
     $calls['upload'][] = $result;
}
$data = array($calls); //put it all into an array
$encode = json_encode($data);
var_dump($encode);

var_dump($encode) 产生

[[{"call_id":"235","caller_id":"23","call_time":"2017-11-01 12:47:27","upload":[{"call_id":"235","upload_id":"145","file_name":"bu2t7384uhjnfns.mp3"},{"call_id":"235","upload_id":"146","file_name":"jbwer8y23gr92o.mp3 "}]}]]' (length=223)

【讨论】:

    【解决方案2】:

    这是您在 $result 中返回的数据。

    $result = array(
    
      array(
        'call_id'   => 235,
        'caller_id' => 23,
        'call_time' => '2017-11-01 12:47:27',
        'upload_id' => 145,
        'file_name' => 'bu2t7384uhjnfns.mp3'
        ),
      array(
        'call_id'   => 235,
        'caller_id' => 23,
        'call_time' => '2017-11-01 12:47:27',
        'upload_id' => 146,
        'file_name' => 'jbwer8y23gr92o.mp3'
       )
     );
    
     //    echo '<pre>';
     //    print_r($result);
    

    这将构建 json 结果。 (我可能错过了一个聪明的现有 php 函数,它可以在没有所有数组等的情况下自动执行此操作。)

     $call_uploads = array();
     $calls = array();
     $uploads = array();
    
     foreach($result as $ups) {
    
       if (empty($call)) {
         $call = array(
           'call_id'   => $ups['call_id'],
           'caller_id' => $ups['caller_id'],
           'call_time' => $ups['call_time']
         );
       }
    
       $upload = array(
         'upload_id' => $ups['upload_id'],
         'call_id'   => $ups['call_id'],
         'file_name' => $ups['file_name']
       );
    
       array_push($uploads, $upload);
    
     };
    
     $call['uploads'] = $uploads;
    
     array_push($calls, $call);
     $call_uploads['calls'] = $calls;
    
     //print_r($call_uploads);
    
     $j = json_encode($call_uploads);
    
     //print_r($j);
    

    抱歉,不知道 Handlebars。

    【讨论】:

      猜你喜欢
      • 2014-01-23
      • 2019-04-29
      • 2016-12-11
      • 2019-05-09
      • 2017-02-15
      • 2020-04-09
      • 2018-09-02
      • 2015-02-09
      • 1970-01-01
      相关资源
      最近更新 更多