1

我正在尝试以有效的方式将 PHP 中的查询结果分组为关联数组。我可以使用多个嵌套的 foreach 循环,检查当前 id 字段是否与每个数组组的前一个匹配,但我猜这个问题有更好/更有效的解决方案。有没有人有一个聪明的解决方案?理想情况下,可以对给定键字段或嵌套键字段数组的任何查询结果集进行分组的通用函数或方法?

这是查询结果数组:

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] => 
        )

)

这是我想将其转换为的数组:

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()
                )
            )
        )
    )
)

分组关系说明:

一个look可以有1个或多个服装物品。一件服装物品可以有一个或多个服装品牌。外观服装文章可以有一个或多个属性。一个服装品牌可以有一个或多个属性。

4

2 回答 2

1

这不应该太可怕。而不是对内部数组使用数字索引,您应该使用唯一标识符,以便您可以对其进行分组。这也不排除您在数组本身中使用该标识符。

//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
于 2013-04-26T21:34:06.650 回答
0

执行与连接级别一样多的查询,每个表一个查询,所有查询都按相同的列排序并将行编织在一起:

$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

或者你可以使用一个现成的ORM库,比如学说

于 2013-04-26T21:45:20.817 回答