简体   繁体   English

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

[英]PHP group query results as nested associative array

I'm trying to group query results in PHP as an associative array in an efficient manner. 我正在尝试以有效方式将PHP中的查询结果分组为关联数组。 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. 我可以使用多个嵌套的foreach循环,检查当前id字段是否与每个数组组的当前id字段匹配,但是我猜想对此问题有更好/更有效的解决方案。 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. 一个外观可以包含1个或多个服装商品。 A clothing article can have 1 or many clothing brands. 服装商品可以具有1个或多个服装品牌。 A look clothing article can have 1 or many attributes. 外观服饰商品可以具有1个或多个属性。 A clothing article brand can have 1 or many attributes. 服装商品品牌可以具有1个或多个属性。

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

Do as many queries as you have join levels, one for each table, all sorted on the same columns and weave the rows together: 进行与连接级别一样多的查询,每个表一个查询,所有查询都排序在同一列上,并将行编织在一起:

$q1 = "SELECT * FROM look ORDER BY look_id";
$q2 = "SELECT look_id, article.* FROM look INNER JOIN article USING(look_id) ORDER BY look_id, article_id";
$q3 = "SELECT look_id, article_id, attribute.* FROM look INNER JOIN article USING(look_id) INNER JOIN attribute USING(article_id) ORDER BY look_id, article_id, attribute_id";

// loop on looks
// inner loop on article and add to look
// inner loop on attribute and add to article

Or you could use a ready made ORM library like doctrine . 或者您可以使用现成的ORM库,例如doctrine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM