从mysql数据库和php获取多级json

ttvkxqim  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(388)

我有table


我想检索一个json字符串的格式

[{
    "topic_id":"1",
    "topic_title":"Input and Output devices",
    "topic_image":"inputs.png",
    "subtopics": {
        "subtopic_id":"1",
        "subtopic_title":"Inputs",
        "subtopic_file_url":"0"
    }, 
    {      
        "subtopic_id":"2",
        "subtopic_title":"Outputs",
        "subtopic_file_url":"0"
    }
}]

我试过的

// set the resulting array to associative
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
foreach ($result as $key => $value) {
  $topic[$key]['topic_id'] = $value['topic_id'];
  $topic[$key]['topic_title'] = $value['topic_title'];
  $topic[$key]['topic_image'] = $value['topic_image'];
  $topic[$key]['subtopics']['subtopic_id'] = $value['subtopicid'];
  $topic[$key]['subtopics']['subtopic_title'] = $value['subtopic_title'];
  $topic[$key]['subtopics']['subtopic_file_url'] = $value['file_url'];
  $topic[$key]['subtopics']['subtopicid'] = $value['subtopicid'];
}
print 'PHP_ARRAY: ';print_r($topic);
$topic = json_encode ( $topic );

上面的脚本返回每个子主题的主题详细信息。

kjthegm6

kjthegm61#

This code has worked. Thank you everyone
 <?php

    $servername = "***";
    $username = "****";
    $password = "***";
    $dbname = "****";

    function searchForId($id, $array) {
       foreach ($array as $key => $val) {
           if ($val['topic_id'] === $id) {
               return $key;
           }
       }
       return null;
    }

    try {
      $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $stmt = $conn->prepare("select a.topic_id, a.topic_title, a.topic_image, b.subtopicid,b.subtopic_title,b.file_url from topics a join subtopics b on b.topic_id = a.topic_id ");
      $stmt->execute();

      // set the resulting array to associative
      $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
      $x = 0;
      foreach ($result as $key => $value) {
        if($x != 0){
          if($topic[$x-1]['topic_id'] != $value['topic_id']) {
            $topic[$x]['topic_id'] = $topic_id = $value['topic_id'];
            $topic[$x]['topic_title'] = $value['topic_title'];
            $topic[$x]['topic_image'] = $value['topic_image'];
            $x++;
          }
        } else {
          $topic[$key]['topic_id'] = $topic_id = $value['topic_id'];
          $topic[$key]['topic_title'] = $value['topic_title'];
          $topic[$key]['topic_image'] = $value['topic_image'];
          $x++;
        }
      }
      $x = $y = 0;
      foreach ($result as $key => $value) {
        if($y != searchForId($value['topic_id'], $topic))
          $x = 0;
        $y = searchForId($value['topic_id'], $topic);
        $topic[$y]['subtopics'][$x]['subtopic_id'] = $value['subtopicid'];
        $topic[$y]['subtopics'][$x]['subtopic_title'] = $value['subtopic_title'];
        $topic[$y]['subtopics'][$x]['subtopic_file_url'] = $value['file_url'];
       // $topic[$y]['subtopics'][$x]['subtopicid'] = $value['subtopicid'];
        $x++;
      }

      // foreach ($result as $key => $value) {

      // }
      //print 'PHP_ARRAY: ';print_r($topic);
      $topic = json_encode ( $topic );
      print_r($topic);
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;
    ?>

相关问题