簡體   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