将MySQL连接转换为JSON数组

时间:2017-12-16 19:29:57

标签: php mysql json codeigniter

我有一个名为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的人的奖励积分:)

2 个答案:

答案 0 :(得分:0)

这是您在$ 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);

抱歉,不知道车把。

答案 1 :(得分:0)

假设$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)