php - 从 mysql db 和 php 获取多级 json

标签 php mysql json encode

我有 table enter image description here

enter image description here

我想检索格式的 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 );

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

最佳答案

       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;
    ?> 

关于php - 从 mysql db 和 php 获取多级 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863286/

相关文章:

mysql - 需要将 1 行数据转置为 1 列但多行

javascript - 来自 2 个 MySQL 查询的数组用于使用 javascript 的 2 个选择

javascript - 对两个 ajax() 请求使用相同的回调会产生 "parsererror"

php - 检测负数

javascript - 在 javascript 中查询 php json_encode 结果

javascript - 如何在ajax调用过滤后使项目可拖动

php - LOAD DATA LOCAL INFILE 不能跳过列?

php - 为什么我的请求 POST swift 到 php,内容总是 PHP 的关键

php - 我们可以在 insert into 语句中传递 where 条件吗?如果不是,Codeigniter 中的解决方案是什么?

java - 如何在 java 中从 postgres 检索和使用 json 列以及非 json 列?