繁体   English   中英

将MySQL连接转换为JSON数组

[英]Convert MySQL join to JSON array

我有一个名为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的键,它是一个来自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的任何人的奖励积分:)

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

抱歉,不认识把手。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM