2013-03-03 2 views
1

timelineJS требует определенного формата JSON. До сих пор с этим PHP код я могу генерировать выходной сигнал, как это показано ниже: Построение многомерного массива для желаемого выхода JSON для timeline.js

$rs = mysql_query("SELECT filename, data AS added, f.image as media, f.info_hash AS hash, c.name AS category FROM xbtit_files f LEFT JOIN xbtit_categories c ON f.category = c.id WHERE f.uploader = '2' ORDER BY added DESC"); 

$json_arr = array(); 
while($row = mysql_fetch_array($rs)) { 
if (strlen(htmlspecialchars($row[filename])) > 30) 
    $t_name = substr(htmlspecialchars($row[filename]), 0, 30)."..."; 
else 
    $t_name = htmlspecialchars($row[filename]); 

$row_arr['credit'] = $row['hash']; 
$row_arr['caption'] = $row['category']; 
$row_arr['media'] = $row['media']; 
$row_arr['headline'] = $t_name; 
$row_arr['startDate'] = $row[added]; 
array_push($json_arr,$row_arr); 
} 
$json = '{"timeline": 
    { 
     "headline":"UPLOADER1", 
     "type":"default", 
     "text":"Show all uploads of this uploader", 
     "asset": { 
      "media":"http://localhost/torrent/torrentimg/49ac3aa95ec6d2ae56772a158b41d4aa62a7b78c.jpg", 
      "credit":"Credit Name Goes Here", 
      "caption":"Caption text goes here" 
      }, 
     "date": 
      '.json_encode($json_arr).' 

    }}'; 
echo $json; 

Это дает выход так:

{ 
"timeline": { 
    "headline": "UPLOADER1", 
    "type": "default", 
    "text": "Show all uploads of this uploader", 
    "asset": { 
     "media": "http://localhost/torrent/torrentimg/49ac3aa95ec6d2ae56772a158b41d4aa62a7b78c.jpg", 
     "credit": "Credit Name Goes Here", 
     "caption": "Caption text goes here" 
    }, 
    "date": [ 
     { 
      "headline": "Avatar", 
      "startDate": "2012-06-26 12:03:13" 
     }, 
     { 
      "headline": "Rio BRRIP x264-1080p-2011", 
      "startDate": "2012-06-26 11:59:19" 
     }, 
     { 
      "headline": "The Number 23 (2007)", 
      "startDate": "2012-06-26 11:50:44" 
     }, 
     { 
      "headline": "Fate Stay night", 
      "startDate": "2012-06-26 11:41:01" 
     } 
    ] 
} 
} 

Как бы я быть в состоянии получить что-то вроде этого:

{ 
"timeline": 
{ 
    "headline":"UPLOADER 1", 
    "type":"default", 
    "text":"Name of the Uploader", 
    "startDate":"2012,1,26", 
    "date": [ 
     { 
      "startDate":"2012,2,30", 
      "headline":"Hanky Panky (Torrent Name)", 
      "text":"<p>Some description of the torrent.</p>", 
      "asset": 
      { 
       "media":"Poster.jpg", 
       "credit":"", 
       "caption":"" 
      } 
     }, 
     { 
      "startDate":"2012,2,18", 
      "headline":"Torrent Name 2", 
      "text":"This movie was released on... The actors are....", 
      "asset": 
      { 
       "media":"Poster1.jpg", 
       "credit":"", 
       "caption":"Directed and Edited by Matt Mayer, Produced by Seth Keim, Written by Eliot Glazer. Featuring Eliot and Ilana Glazer, who are siblings, not married." 
      } 
     } 
    ] 
} 
} 

Обратите внимание: в объекте есть объекты JSON. Как это может быть достигнуто? Может кто-нибудь помочь?

ответ

1

Я нашел решение, переделав массивы, как предложено user1660584. Благодарю.

многомерный массив будет выглядеть следующим образом:

$c['timeline']['headline']= 'uploader1'; 
$c['timeline']['type'] = 'Default Type'; 
$c['timeline']['text'] = 'Some Text about the uploader'; 
$c['timeline']['asset']['media'] = 'torrentimg/49ac3aa95ec6d2ae56772a158b41d4aa62a7b78c.jpg'; 
$c['timeline']['asset']['caption'] = 'Assets consists of media files, caption and categories'; 
$c['timeline']['date'] = array(array(
    "startDate"=>'asfsf as fa', 
    "headline"=>'kjahdas a', 
    "text"=>'ahidahs kahs', 
    "asset" => array(
       "media"=> 'image.png', 
       "credit"=> 'Category', 
       "caption"=> 'Some Text' 
    ) 
),array(
    "startDate"=>'asfsf as fa', 
    "headline"=>'kjahdas a', 
    "text"=>'ahidahs kahs', 
    "asset" => array(
       "media"=> 'image.png', 
       "credit"=> 'Category', 
       "caption"=> 'Some Text' 
    ) 
)); 
echo json_encode($c); 

Я просто поставить его в петлю с SQL relult.

2

json_encode() может преобразовывать многомерный массив PHP в строку json без проблем, поэтому создайте массив PHP с требуемой структурой и вызовите json_encode на нем.

+0

Вот где я застрял. Я не могу понять, как я буду строить многомерный массив. – Imtiaz

+1

хорошо, что делает его совершенно другим вопросом. $ a = array(); $ a ['firstKey'] = array(); $ a ['firstKey'] ['secondKey'] = 'theValue'; – amik

Смежные вопросы