我想从平面结构创建一个JSON树 - 在本例中是一个App.net线程。
我想要像这样的JSON
"id": "12345",
"name": "Ringo",
"data":
{
"avatar": "",
"text": "We All Live",
},
"children": [{
"id": "34567",
"name": "John",
"data":
{
"avatar": "",
"text": "In a pink submarine?",
},
"children": [{
"id": "35555",
"name": "George",
"data":
{
"avatar": "",
"text": "Don't be daft",
},
"children": []
}]
},{
"id": "98765",
"name": "Paul",
"data":
{
"avatar": "",
"text": "In a yellow submarine?",
},
"children": []
}]
所以,每个帖子都可以有多个孩子。每个孩子都可以生孩子。
从App.net回来的JSON是不线程。
{
"id": "98765",
"parent": "12345"
"details": {
...}
},
{
"id": "34567",
"parent": "12345"
"details": {
...}
},
我已经使用json_decode()来获取数组中的JSON响应。我可以使用foreach迭代。
如何将每个帖子放在多维数组的正确部分?
Parent
|_
|-child
|-child
| |-child
|-child
等
答案 0 :(得分:4)
我会使用引用,经常被遗忘的PHP硬链接。像这样:
我假设您有一个$posts
数组,您已从App.net API调用中获得回复。
(未经测试,可能无法编译/运行/可能有错误/可能是更有效的方法来执行此操作)
// first throw everything into an associative array for easy access
$references = array();
foreach ($posts as $post) {
$id = $post['id'];
$post['children'] = array();
$references[$id] = $post;
}
// now create the tree
$tree = array();
foreach ($references as &$post) {
$id = $post['id'];
$parentId = $post['parent'];
// if it's a top level object, add it to the tree
if (!$parentId) {
$tree[] =& $references[$id];
}
// else add it to the parent
else {
$references[$parentId]['children'][] =& $post;
}
// avoid bad things by clearing the reference
unset($post);
}
// encode it
print json_encode($tree);
答案 1 :(得分:0)
在这里草拟答案:假设您可以在RAM中保留所有条目,否则您将不得不做出一些排序假设,并在完整单元完成后清除我们的阵列。
创建一个由id保持结构索引的数组posts
,其中包含详细信息和子数组。
然后迭代输入数组和每个元素:
posts[id]
(如果尚未创建)post[id]
posts[parent_id]
并将此结构添加到那里的孩子。最后,您可以遍历所有帖子,而那些没有父母的帖子是他们的孩子正确填写的根。
答案 2 :(得分:0)
我写了class and example script来完成我认为你想要的东西。
它将平面结构转换为分层结构,并且还考虑了孤立更新(没有父更新的结构)。
<强> Update.php 强>
<?php
/**
* An App.net update.
*/
class Update extends ArrayObject
{
/**
* The update's children.
*
* @var array
*/
private $children = array();
/**
* The parent update.
*
* @var Update
*/
private $parent;
/**
* Adds a child to this update.
*
* @param Update The child update.
*/
public function addChild(self $child)
{
$child->setParent($this);
$this->children[] = $child;
}
/**
* Sets the parent update.
*
* @param Update The parent update.
*/
public function setParent(self $parent = null)
{
$this->parent = $parent;
}
/**
* Converts the update and its children to JSON.
*
* @param boolean $encode Automatically encode?
*
* @return string The JSON-encoded update.
*/
public function toJson($encode = true)
{
$data = $this->getArrayCopy();
if ($this->children) {
$data['children'] = array();
foreach ($this->children as $child) {
$data['children'][] = $child->toJSON(false);
}
}
if ($encode) {
return json_encode($data);
}
return $data;
}
}
<强> build.php 强>
<?php
require 'Update.php';
$updates = <<<UPDATES
[
{
"id": "12345",
"name": "Ringo",
"data": {
"avatar": "",
"text": "We All Live"
}
},
{
"id": "34567",
"parent": "12345",
"name": "John",
"data": {
"avatar": "",
"text": "In a pink submarine?"
}
},
{
"id": "98765",
"parent": "12345",
"name": "Paul",
"data": {
"avatar": "",
"text": "In a yellow submarine?"
}
}
]
UPDATES;
$original = json_decode($updates, true);
$parents = array();
$children = array();
foreach ($original as $update) {
if (empty($update['parent'])) {
$parents[$update['id']] = $parent = new Update($update);
if (isset($children[$update['id']])) {
foreach ($children[$update['id']] as $child) {
$parent->addChild($child);
}
unset($children[$update['id']]);
}
} else {
$child = new Update($update);
if (isset($parents[$update['parent']])) {
$parents[$update['parent']]->addChild($child);
} else {
if (false === isset($children[$update['parent']])) {
$children[$update['parent']] = array();
}
$children[$update['parent']][] = $child;
}
}
}
// Anything in children at this point are orphans
echo "Parents:\n";
foreach ($parents as $parent) {
echo $parent->toJson();
}
echo "\n\nOrphans:\n";
foreach ($children as $parent => $orphans) {
foreach ($orphans as $orphan) {
echo $orphan->toJson();
}
}