gpt4 book ai didi

php - JIT 为组织结构图构建功能性 json

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

我想构建 JIT SpaceTree 所需的 json 请求。

工作流程:问题以存储的回顾答案的 ID 开始。

  • 如果选项为 yes,则会根据 load_yes 值加载下一个问题。此 load_yes 值选择 ID 并加载该问题。

  • 如果选项为“否”,则会通过 load_no 下存储的 id 加载下一个无基础问题,

json 应该如下所示:

var json = {
id: "start",
name: "does test work?",
data: {},
children: [{
id: "layer1_1",
name: "option: no, id 3, Q: test does work with option no?",
data: {},
children: []
}, {
id: "layer1_2",
name: "option: yes, id 2, Q: test does work!!",
data: {},
children: [{
id: "layer2_1",
name: "option: no, id 4, Q: test does work?",
data: {},
children: []
}, {
id: "layer2_2",
name: "option: yes, id 5, Q: ",
data: {},
children: []
}]
}]
};
<小时/>
$query = $mysqli->query("SELECT * FROM topic_answer");

while($obj = $query->fetch_object()){
$arr[] = array(
'id' => $obj->id,
'name' => $obj->topic_question,
'data' => '',
'children' => array(array(
'id' => $obj->load_yes,
'name' => $obj->load_yes,
'data' => '',
'children' =>array(),
),array(
'id' => $obj->load_no,
'name' => $obj->load_no,
'data' => '',
'children' => array(),

),
)
);
<小时/>
     id, topic_name,  topic_creator, topic_question,  load_yes,  start,  load_no,   end 
1 test jordan does test work? 2 1 3 0
4 test jordan test does work no 0 0 0 0
5 test jordan test does work yes 0 0 0 0
2 test jordan test does work yes!! 4 0 5 0

最佳答案

在 json 中,对象被描述为 {}。所以基本上这段代码的意思是一个对象数组:

[
{},
{}
]

所以在这里,你嵌套数组的地方:

'children' => array(array(
'id' => $obj->load_yes,
'name' => $obj->load_yes,
'data' => '',
'children' =>array(),
),array(
'id' => $obj->load_no,
'name' => $obj->load_no,
'data' => '',
'children' => array(),
),
)

你实际上想用一个对象替换内部数组,例如stdClass()。 stdClass 的使用方式如下:

$obj = new stdClass();
$obj->id = "layer1_1";
$obj->name = "option: no, id 3, Q: test does work with option no?";
$obj->data = new stdClass();
$obj->children = array();

然后,用php

'children' => array(
$obj1,
$obj2,
),

关于php - JIT 为组织结构图构建功能性 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33394895/

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