2016-08-22 6 views
0

Smartyでデータをリストする機能を作成します。それはうまく動作しますが、私の問題は私がデータを2回取得していることです。Jsonエンコード反復データの取得

My機能は以下の通りです: - 私は取得しています

function appListing($requestvars) 
{ 

     try 
     { 
      $rh = $this->db->prepare('SELECT * FROM app'); 
      //$rh->bindParam(':userOwnerId', $_SESSION['userId'], PDO::PARAM_STR); 
      $rh->execute(); 
      $appData['App'] = $rh->fetchAll(); 
      $this->smartyTemplate->assign('appData', $appData); 
      $this->smartyTemplate->assign('request', $requestvars); 
      $this->smartyTemplate->assign('homePath', APP_ROOT_DIR); 
      //$this->smartyTemplate->display('project/appList.html'); 
     } 
     catch (PDOException $e) 
     { 
      $appData = "Error!: " . $e->getMessage(); 
     } 
    echo json_encode($appData); 
} 

出力は以下の通りです: -

{ 
"App": [ 
{ 
    "0": "app_57ba9fc847dd55_57218508", 
    "1": "旅行台南", 
    "2": "https://play.google.com/", 
    "3": "https://play.google.com/1471848392.png", 
    "4": "1471848392", 
    "5": "1471848392", 
    "appId": "app_57ba9fc847dd55_57218508", 
    "appName": "旅行台南", 
    "appURL": "https://play.google.com/", 
    "appImage": "https://play.google.com/1471848392.png", 
    "createTime": "1471848392", 
    "lastUpdateTime": "1471848392" 
} 

私のように出力したい: -

{ 
"App": [ 
{ 
    "appId": "app_57ba9fc847dd55_57218508", 
    "appName": "旅行台南", 
    "appURL": "https://play.google.com/", 
    "appImage": "https://play.google.com/1471848392.png", 
    "createTime": "1471848392", 
    "lastUpdateTime": "1471848392" 
} 

私を助けてください...ありがとう

答えて

2

PDO::FETCH_ASSOCfetchAll()のデフォルトはPDO::FETCH_BOTHで、数値と名前付きの両方のインデックスを持つ配列を返します。

 $appData['App'] = $rh->fetchAll(PDO::FETCH_ASSOC); 
+0

ありがとうございます...その作業 –