-1

certianオブジェクトの子孫をすべて返し、すべてをJSONにエンコードすることになっている関数に問題があります。現在は機能していますが、JSONエンコードに多数のnullがあり、それらをスクラブするためarray_filter()foreachループを試しましたが、どちらも機能しませんでした。

findChildren($conn, $topic);

$data = array();
function findChildren($conn, $topic) {
   $rst = $conn->query("SELECT topicID, topicTitle, topicParentID, topicDeleted FROM tbl_topics WHERE topicParentID = $topic");
   while ($row = $rst->fetch_assoc()) {
      if ($row['topicDeleted'] == 0) {
         //$data[] = htmlentities($row, UTF-8);
         if($row != '') {
            $data[] = $row;   
         }

         findChildren($conn, $row['topicID']);
      }              
   }
   echo json_encode( $data );
}

どんな助けでも素晴らしいでしょう。ありがとう。

4

1 に答える 1

0
<?php

$data = findChildren($conn, $topic);
echo json_encode($data);

function findChildren($conn, $topic) {
    $data = array();
    $rst  = $conn->query(
        "
            SELECT topicID, topicTitle, topicParentID, topicDeleted
            FROM tbl_topics
            WHERE topicParentID = ${topic} and topicDeleted = 0
        "
    );

    while ($row = $rst->fetch_assoc()) {
        $data[] = array_filter($row);

        if(!empty($row['topicID'])) {
            $data = array_merge(
                $data,
                findChildren($conn, $row['topicID'])
            );
        }
    }

    return $data;
}
  • 直接エコーするのではなく、配列を返すように、またはグローバルであるかのように変数を使用するように関数を変更しました
  • 'topicDeleted'チェックをクエリに移動しました
于 2012-10-12T22:00:46.343 に答える