我有具有这种格式标签的任务:
Array
(
[0] => stdClass Object
(
[task_id] => 10,
[task_text] => Mow and fertilize,
[tags] => Array
(
[0] => stdClass Object
(
[tag_id] => 1,
[tag_text] => House,
[tt_tag_id] => 1, //from relational table, tt = task_tag
[tt_task_id] => 10,
[tt_id] => 20, //auto id from relational table
[tt_order] => 0
),
[1] => stdClass Object
(
[tag_id] => 2,
[tag_text] => Yard,
[task_tag_id] => 2,
[task_task_id] => 10,
[tt_id] => 21,
[tt_order] => 1
)
[2] => stdClass Object
(
[tag_id] => 5,
[tag_text] => Lawn,
[task_tag_id] => 5,
[task_task_id] => 10,
[tt_id] => 22,
[tt_order] => 2
)
)
)
[1] => stdClass Object
(
[task_id] => 11,
[task_text] => Unclog the sink drain,
[tags] => Array
(
[0] => stdClass Object
(
[tag_id] => 1,
[tag_text] => House,
[task_tag_id] => 1,
[task_task_id] => 11
[tt_id] => 30,
[tt_order] => 0
),
[1] => stdClass Object
(
[tag_id] => 3,
[tag_text] => Kitchen,
[task_tag_id] => 3,
[task_task_id] => 11,
[tt_id] => 31,
[tt_order] => 1
)
)
)
[2] => stdClass Object
(
[task_id] => 12,
[task_text] => Purchase new microwave,
[tags] => Array
(
[0] => stdClass Object
(
[tag_id] => 4,
[tag_text] => Apartment 1,
[task_tag_id] => 4,
[task_task_id] => 12
[tt_id] => 40,
[tt_order] => 0
),
[1] => stdClass Object
(
[tag_id] => 3,
[tag_text] => Kitchen,
[task_tag_id] => 3,
[task_task_id] => 12,
[tt_id] => 41,
[tt_order] => 1
)
)
)
)
我希望能够按这样的标签对任务进行排序:
Array
(
[0] => stdClass Object
(
[tag_id] => 1,
[task_text] => House,
[child_tags] => Array
(
[0] => stdClass Object
(
[tag_id] => 2,
[tag_text] => Yard,
[task_tag_id] => 2,
[task_task_id] => 10,
[tt_id] => 21,
[child_tags] => Array
(
[1] => stdClass Object
(
[tag_id] => 5,
[tag_text] => Lawn,
[task_tag_id] => 5,
[task_task_id] => 10,
[tt_id] => 22,
[task_id] => 10,
[task_text] => Mow and fertilize
)
),
[1] => stdClass Object
(
[tag_id] => 3,
[tag_text] => Kitchen,
[task_tag_id] => 3,
[task_task_id] => 11,
[tt_id] => 31,
[task_id] => 11,
[task_text] => Unclog the sink drain,
)
)
)
[1] => stdClass Object
(
[tag_id] => 4,
[tag_text] => Apartment 1,
[child_tags] => Array
(
[0] => stdClass Object
(
[tag_id] => 2,
[tag_text] => Kitchen,
[task_tag_id] => 2,
[task_task_id] => 10,
[tt_id] => 21,
[task_id] => 12,
[task_text] => Purchase new microwave
)
)
)
)
最终我要拍摄的是:
<ul>
<lh>House</lh>
<li>
<ul>
<lh>Kitchen</lh>
<li>Unclog the sink drain</li>
</ul>
</li>
<li>
<ul>
<lh>Yard</lh>
<li>
<ul>
<lh>Lawn</lh>
<li>Mow and fertilize</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<lh>Apartment 1</lh>
<li>
<ul>
<lh>Kitchen</lh>
<li>Purchase new microwave</li>
</ul>
</li>
</ul>
数据库正是您想象的样子。一个任务表和一个带有 n2n 关系表的标签表。
似乎没有一种简单的方法可以循环遍历任务和标签,从而以这种方式对它们进行排序。我应该尝试不同的方法吗?这很傻吗?