我使用的是邻接模型列表,例如:
母插孔id 1
children some_name parent_id 1
children some_name parent_id 1
children some_name parent_id 1
因此,当我进行查询时,假设深度约为1 lvl,它会呈现以下内容:
jack->some_name
jack->some_name
jack->some_name
但是我怎么能把它渲染成这样呢:
插孔->{
some_name
some_name
some_name
}
有可能吗?,因为关键字DISTINCT和GROUP BY没有帮助:(
考虑使用JSON来格式化数据。
使用json_encode:
$persons = array ();
while ($row = mysql_fetch_assoc($result))
{
if (empty($persons[$row['person']]))
$persons[$row['person']] = array ();
array_push ($persons[$row['person']], $row['child']);
}
echo json_encode ($persons);
它应该产生以下结果:
{
'jack': [
'some_name',
'some_name',
'some_name'
]
}
大部分代码来自https://stackoverflow.com/a/1951130/443219