将平面json转换为像json一样的树视图

时间:2014-05-07 10:06:44

标签: php mysql json

            while($row = mysqli_fetch_assoc($result)) { 
              echo (json_encode($row));  
            }   

echo产生:

            {"name":"REPORTING","parent":"null","children":"BO"}{"name":"IHS","parent":"BO","children":"1"}{"name":"TOMCAT","parent":"BO","children":"1"}{"name":"WAS","parent":"BO","children":"1"}{"name":"BO","parent":"BO","children":"1"}{"name":"1","parent":"IHS","children":"APP NAME"}{"name":"1","parent":"TOMCAT","children":"APP NAME"}{"name":"1","parent":"WAS","children":"APP NAME"}{"name":"1","parent":"BO","children":"APP NAME"}       

我正在寻找的是:

[
  {
    "name": "REPORTING",
    "parent": "null",
    "children": [
      {
        "name": "BO",
        "parent": "REPORTING",
        "children": [
          {
            "name": "I H S",
            "parent": "BO",
            "children": [
          {
            "name": "34534",
            "parent": "BO",
            "children": [

          {
            "name": "Application Name",
            "parent": "34534",

          }
        ]
          },
          {
            "name": "34535",
            "parent": "BO",
            "children": [

          {
            "name": "Application Name",
            "parent": "34535",

          }
        ]

          },
          {
            "name": "34536",
            "parent": "BO",
            "children": [

          {
            "name": "Application Name",
            "parent": "34536",

          }
        ]

2 个答案:

答案 0 :(得分:1)

您不能将行转储到json_encode回显中,因为您的数据与您所需结果中的JSON结构不同。您必须根据父/子值编写构造树的代码。我建议您创建一个包含所需字段的类,并在循环中填充数据。

一个简单的例子(未完成的代码,意味着只是对方法有所了解):

require_once ('MyClass.php')
$data = new MyClass();
while($row = mysqli_fetch_assoc($result)) {
    if($row['parent'] == 'null') {
        $data->parse($row);
    } else {
        $child = $data->findChild($row['parent']);
        if($child !== false) {
            $child->parseChild($row);
        }
    }
}
echo json_encode($data);

MyClass.php:

class MyClass {
    public $name;
    public $parent;
    public $children;

    public function __construct($name = "") {
        $this->name = $name;
        $this->parent = "null";
        $this->children = array();
    }

    public function parse($rowdata) {
        $this->name = $rowdata['name'];
        $this->parent = $rowdata['parent'];
        echo "Created object " . $this->name . "\n";
    }

    public function parseChild($rowdata) {
        $child = new MyClass();
        $child->parse($rowdata);
        $this->children[] = $child;
    }

    public function findChild($name) {
        if($this->name == $name) {
            return $this;
        } else {
            foreach($this->children as $child) {
                if($child->name == $name) {
                    return $child;
                } else {
                    $ch = $child->findChild($name);
                    if($ch !== false) {
                        return $ch;
                    }
                }
            }
        }
        return false;
    }
}

此代码存在缺陷和错误,例如父母需要在孩子面前。在您的示例echo中,IHS在BO之前呈现,因此没有这样的父级,并且无法创建子级。您需要正确订购数据才能使此代码正常工作。

答案 1 :(得分:0)

以前我还必须使用平面数据结构。我用PHP中的处理器密集型递归函数解决了这个问题。

例如(注释代码未经测试);

PHP:

// MySQL connection defined elsewhere as $con and assuming your table is called Categories
function getCategories($name = null) {
    if(is_null($name)){
        //get all top-level parents
        $sqlString = "SELECT * FROM Categories WHERE parent IS NULL";
    } else {
        $sqlString = "SELECT * FROM Categories WHERE parent = '$name'";
    }

    // Initialise empty categories array;
    $allCats = array();

    $result = mysqli_query($con, $sqlString);

    while ($row = mysqli_fetch_array($result)) {

        $cat['name'] = $row['name'];
        $cat['parent'] = $row['parent'];

        // Fetch any children it may have
        $childCats = getCategories($row['name']);
        if (count($childCats) > 0) {
            $childCat['children'] = $childCats;
        }

        $cat['children'][] = $childCat;
        $allCats['children'][] = $cat;
    }

    return $allCats;
}

$allCats['name'] = "All Categories";
$allCats['parent'] = null;
$allCats['children'] = getCategories();

echo json_encode($allCats);