gpt4 book ai didi

php - 我如何使用 php 生成 fancytree json 格式

转载 作者:行者123 更新时间:2023-11-29 18:06:25 25 4
gpt4 key购买 nike

我是第一次使用fancytree,我发现为fancytree生成Json结果有些困难。我有一个数据库表包含 id、name、desc、parent_id。

我正在开发 codeigniter 这是我的代码:

public function my_tree(){
$this->data['tree'] = array();
$res = $this->crud->read('dbtree')->result_array();
//iterate on results row and create new index array of data
foreach($res as $value){
$this->data['tree'] = $res;
}
$itemsByReference = array();
// Build array of item references:
foreach($this->data['tree'] as $key => &$item) {
$itemsByReference[$item['id']] = &$item;
// Children array:
$itemsByReference[$item['id']]['children'] = array();
// Empty data class (so that json_encode adds "data: {}" )
$itemsByReference[$item['id']]['data'] = new StdClass();
}

// Set items as children of the relevant parent item.
foreach($this->data['tree'] as $key => &$item)
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
$itemsByReference [$item['parent_id']]['children'][] = &$item;

// Remove items that were added to parents elsewhere:
foreach($this->data['tree'] as $key => &$item) {
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
unset($this->data['tree'][$key]);
}
// Encode:
$this->data['page'] = "server_tree";
$this->load->view('layout', $this->data);
}

我如何使用 php 循环制作这个 key :

[
{"title": "Expanded folder with children", "expanded": true, "folder": true, "children": [
{"key": "1_2", "title": "Expanded sub-item", "expanded": true, "children": [
{"key": "1_2_1", "title": "Active sub-item (active and focus on init)", "active": true, "focused": true},
{"key": "1_2_2", "title": "Basic <i>menu item</i> with <strong class='text-semibold'>HTML support</strong>"}
]},
{"key": "1_3", "title": "Expanded sub-item", "children": [
{"key": "1_3_1", "title": "Sub-item 2.2.1"},
{"key": "1_3_2", "title": "Sub-item 2.2.2"}
]}
]},
{"key": "2", "title": "Menu item with key and tooltip", "extraClasses": "has-tooltip", "tooltip": "Look, a tool tip!"},
{"key": "3", "title": "Collapsed folder", "folder": true, "children": [
{"key": "3_1", "title": "Sub-item 1.1"},
{"key": "3_1", "title": "Sub-item 1.2"}
]},
{"key": "4", "title": "This is a selected item", "selected": true},
{"key": "5", "title": "Document with some children (expanded on init)", "expanded": true, "children": [
{"key": "5_1", "title": "Document sub-item"},
{"key": "5_2", "title": "Another document sub-item", "children": [
{"key": "5_2_1", "title": "Sub-item 2.1.1"},
{"key": "5_2_2", "title": "Sub-item 2.1.2"}
]}
]}

]

最佳答案

您可以使用Marshal Serializer轻松创建结构。

然后将生成的数组传递给 json_encode 函数,然后将其用于 fancytree。

TreeMapper.php

namespace MyApp\Mapper;

use KingsonDe\Marshal\AbstractMapper;
use MyApp\Data\Tree;

class TreeMapper extends AbstractMapper {

public function map(Tree $tree) {
$output = [
'key' => $tree->getKey(),
'title' => $tree->getTitle(),
];

if ($tree->isFolder()) {
$output['folder'] = true;
$output['children'] = $this->collection($this, $tree->getChildren());
}

return $output;
}
}

fancytree.php

use KingsonDe\Marshal\Marshal;
use MyApp\Mapper\TreeMapper;

$treeCollection = getFancyTreeCollection();

$data = Marshal::serializeCollection(new TreeMapper(), $treeCollection);

echo json_encode($data);

关于php - 我如何使用 php 生成 fancytree json 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47777637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com