PHP的 - 创建一个从MySQL数据嵌套数组 [英] PHP - Create a nested array from MySQL data

查看:262
本文介绍了PHP的 - 创建一个从MySQL数据嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在一个表像这样一些数据:

I have some data stored in a table like so:

id  parent_id  name
1   0          Entry 1
2   0          Entry 2
3   0          Entry 3
4   1          Child of entry 1

我要变成一个嵌套数组,像这样:

I want to turn it into a nested array like so:

array(
    array(
        'id' => 1,
        'parent_id' => 0,
        'name' => 'Entry 1',
        'children' => array(...)
    ),
    ...
);

在理想情况下,就需要支持嵌套(儿童儿童)无限量。是我的表设置为支持这一点,如果是这样,我怎么会产生使用表中的数据这类型的数组?如果没有,我应该怎么设置我的表?

Ideally, it would need to support an infinite amount of nesting (children with children). Is my table set up to support this, if so, how would I generate this kind of array using the data in the table? If not, how should I set up my table?

推荐答案

这里有mysql中管理分层数据很好的说明:
管理分层数据
这里是建设嵌套阵列的另一个很好的例子:
建筑嵌套阵列

There is a very good description of managing hierarchical data in mysql here: managing hierarchical data Here is another good example of building nested arrays: building nested arrays

您可以考虑使用嵌套集模型。如果你想查询的东西mutch它比你现在使用的邻接模式更好。

You may think about using the Nested Set model. If you are going to query stuff mutch it is better than the adjacency model you are using right now.

希望有所帮助。

这篇关于PHP的 - 创建一个从MySQL数据嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆