Php 将平面json转换为类似json的树视图
Php 将平面json转换为类似json的树视图,php,mysql,json,Php,Mysql,Json,echo产生: while($row = mysqli_fetch_assoc($result)) { echo (json_encode($row)); } 我要找的是: {"name":"REPORTING","parent":"null","children":"BO"}{"name":"IHS","parent":"BO","children":"1"}{"name":"TO
echo
产生:
while($row = mysqli_fetch_assoc($result)) {
echo (json_encode($row));
}
我要找的是:
{"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"}
您不能将行转储到
json\u encode
echo中,因为您的数据的结构与所需结果中的json不同。您必须编写基于父/子值构造树的代码。我建议您使用所需的字段创建一个类,并在循环中填充数据
一个简单的示例(不是完成的代码,只是为了让您对该方法有一些了解):
MyClass.php:
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);
这段代码有缺陷和bug,比如父母必须先于孩子。在您的示例中,
echo
IHS出现在BO之前,因此没有这样的父对象,无法创建子对象。您需要对数据进行正确排序,才能使此代码正常工作。之前,我还必须使用平面数据结构来完成此操作。我用PHP中的处理器密集型递归函数解决了这个问题
例如(注释代码未测试)
PHP:
到底是什么问题?你不能使用
JSON_PRETTY_PRINT
,因为你的PHP版本太旧了?我想要你在“我要找的是:”中看到的代码,作为输出,我使用d3js@ÁlvaroG.VicarioJSON_PRETTY_PRINT
是不够的,因为行之间没有子/父关系,其实我对php和d3js还不太熟悉,请检查小提琴。请提供一些链接,以便我可以写所需的代码
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);
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;
}
}
// 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);