doucuo8618 2013-04-26 21:19
浏览 62
已采纳

PHP组查询结果为嵌套关联数组

I'm trying to group query results in PHP as an associative array in an efficient manner. I could use multiple nested foreach loops, checking if the current id field matches the previous for each array group, but I'm guessing there are better/more efficient solutions to this problem. Does anyone have a clever solution for this? Ideally a generic function or method that can group any query result set given a key field or array of nested key fields?

Here is the query results array:

Array
(
    [0] => Array
        (
            [look_id] => 3
            [look_name] => Test Look
            [look_description] => description here
            [clothing_article_id] => 1
            [clothing_article_name] => Coat
            [look_clothing_article_attribute_id] => 1
            [look_clothing_article_attribute_name] => Purple
            [clothing_brand_name] => Gap
            [clothing_article_brand_price] => 40.00
            [clothing_article_brand_attribute_price] => 50.00
            [clothing_article_brand_attribute_name] => Purple
        )

    [1] => Array
        (
            [look_id] => 3
            [look_name] => Test Look
            [look_description] => description here
            [clothing_article_id] => 1
            [clothing_article_name] => Coat
            [look_clothing_article_attribute_id] => 2
            [look_clothing_article_attribute_name] => Black
            [clothing_brand_name] => Gap
            [clothing_article_brand_price] => 40.00
            [clothing_article_brand_attribute_price] => 
            [clothing_article_brand_attribute_name] => 
        )

    [2] => Array
        (
            [look_id] => 3
            [look_name] => Test Look
            [look_description] => description here
            [clothing_article_id] => 2
            [clothing_article_name] => Pants
            [look_clothing_article_attribute_id] => 3
            [look_clothing_article_attribute_name] => Cuffed
            [clothing_brand_name] => 
            [clothing_article_brand_price] => 
            [clothing_article_brand_attribute_price] => 
            [clothing_article_brand_attribute_name] => 
        )

)

And this is the array I'd like to convert it to:

Array
(
    'looks' => Array(
        [0] => Array(
            'id' => 3,
            'name' => 'Test Look',
            'description' => 'description here',
            'clothingArticles' => Array(
                [0] => Array(
                    'id' => 1,
                    'name' => 'Coat',
                    'attributes' => Array(
                        [0] => Array(
                            'id' => 1,
                            'name' => 'Purple'
                            'brands' => Array(
                                [0] => Array(
                                    'name' => 'Gap',
                                    'price' => 50.00
                                )
                            )
                        ),
                        [1] => Array(
                            'id' => 2,
                            'name' => 'Black'
                        )
                    ),
                    'brands' => Array(
                        [0] => Array(
                            'name' => 'Gap',
                            'price' => 40.00
                        )
                    )
                ),
                [1] => Array(
                    'id' => 2,
                    'name' => 'Pants',
                    'attributes' => Array(
                        [0] => Array(
                            'id' => 3,
                            'name' => 'Cuffed'
                            'brands' => Array()
                        )
                    ),
                    'brands' => Array()
                )
            )
        )
    )
)

Explanation of grouping relationships:

A look can have 1 or many clothing articles. A clothing article can have 1 or many clothing brands. A look clothing article can have 1 or many attributes. A clothing article brand can have 1 or many attributes.

  • 写回答

2条回答 默认 最新

  • dshdsh2016 2013-04-26 21:34
    关注

    This shouldn't be too scary to do; rather than using numeric indices for the inner arrays you should use whatever the unique identifier is so you can group on that. This doesn't preclude you from using that identifier in the array itself either.

    //seems a little pointless?
    $looks = array('looks' => array());
    $looks = &$looks['looks'];
    while ($row = $result->fetch()) {
        if (!isset($looks[$row->look_id])) {
            $looks[$row->look_id] = array(
                'id' => $row->look_id,
                'name' => $row->look_name,
                'description' => $row->look_description,
                'clothingArticles' => array()
            );
        }
        $look = &$looks[$row->look_id];
        if (!isset($look[$row->clothing_article_id])) {
            //continue this process as needed
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)