0

データベース テーブルの 1 つを取得し、完全な 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';
    }
}

この配列から、ここで概説する方法を使用してテーブルが生成されます。次に、テーブルソーターをテーブルに適用します。私の質問は、現在、このスクリプトはすべての行と列を出力するということです。例:

[{"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ましたが、うまくいきませんでした。

4

2 に答える 2

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

*を出力したい列のみのリストに変更すると、バックエンドで配列に煩わされることを心配する必要がなくなります。

于 2013-08-03T01:51:10.330 に答える
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';
    }
}
于 2013-08-03T03:50:21.633 に答える