2013-08-03 60 views
0

我寫了一個簡單的函數,它接受我的一個數據庫表並輸出一個完整的JSON數組。JSON數組到表排除列

function tableJSON ($table, $orderby) { 
    $array = array(); 
    $sql = Nemesis::select("*", $table, NULL, $orderby); 
    if ($sql) { 
     if ($sql->num_rows > 0) { 
      while ($row = $sql->fetch_assoc()) { 
       // push row values to array 
       array_push($array, $row); 
      } 
      return json_encode($array); 
     } else { 
      echo 'Query returned empty'; 
     } 
    } else { 
     echo 'Query error'; 
    } 
} 

從該陣列使用一種方法生成一個表中概述here。然後我將桌子分揀機應用到桌子上。我的問題是,目前,這個腳本輸出所有的行和列。例如:

[{"id":"109225488","project_name":"One on One Interview the Dean of RSM","project_bold":"Interview","project_content":"Interview with the Dean of Erasmus University Rotterdam School of Management. Interviewer: Joost Kammermans.","project_image_1":"\/images\/uploads\/projects\/109225488\/m_109225488_1.jpg","project_image_2":"","project_image_3":"","project_image_4":"","youtube_link":"http:\/\/www.youtube.com\/watch?v=9rsR3FcLAxI","published":"1","created":"2013-05-29 14:07:49","created_by":"1","last_modified":"2013-07-22 19:43:15","last_modified_by":"1"} 

如何從輸出排除列的數組中排除此腳本?

例如:

$excluded = array('created_by', 'project_image_1');

我已經試過array_diff,沒有運氣。

回答

2
$sql = Nemesis::select("*", $table, NULL, $orderby); 

更改*僅列的列表中選擇所需的輸出,你將不必擔心與在後端陣列大驚小怪。

+0

有趣的問題,但在我自己的利益,我很好奇。生病upvote在任何情況下 – Alex

+0

我同意 - 只處理你需要的列是你最好的選擇 - 你不必以一種奇怪的方式篩選數據。 – phatskat

0

我建議DevIshOne的答案,但...

function tableJSON ($table, $orderby, $excluded = array()) { 
    $array = array(); 
    $sql = Nemesis::select("*", $table, NULL, $orderby); 
    if ($sql) { 
     if ($sql->num_rows > 0) { 
      while ($row = $sql->fetch_assoc()) { 
       $newrow = array(); 

       foreach($row as $col => $val) 
        if(!in_array($col, $excluded)) 
         $newrow[$col] = $val; 
       // push row values to array 
       array_push($array, $newrow); 
      } 
      return json_encode($array); 
     } else { 
      echo 'Query returned empty'; 
     } 
    } else { 
     echo 'Query error'; 
    } 
}