php多维数组分组

时间:2016-03-02 09:58:22

标签: php arrays multidimensional-array

我有一个包含衣服AttributesVariations的数组,每个属性都有未知数量的变体。

我拥有的数组是......

array(
(int) 0 => array(
    'Variation' => array(
        'title' => '40 inch'
    ),
    'Attribute' => array(
        'title' => 'Size'
    )
),
(int) 1 => array(
    'Variation' => array(
        'title' => '42 inch'
    ),
    'Attribute' => array(
        'title' => 'Size'
    )
),
(int) 2 => array(
    'Variation' => array(
        'title' => 'Long'
    ),
    'Attribute' => array(
        'title' => 'Length'
    )
),
(int) 3 => array(
    'Variation' => array(
        'title' => 'Short'
    ),
    'Attribute' => array(
        'title' => 'Length'
    )
)
)

我尝试对其进行分组并重新排序以提供此

array(
(int) 0 => array(
    'Attribute' => array(
        'title' => 'Size',
    ),
    'Variation' => array(
        (int) 0 => array(
            'title' => '40 inch',
        ),
        (int) 1 => array(
            'title' => '42 inch',
        )
    )
),
(int) 1 => array(
    'Attribute' => array(
        'title' => 'Length',
    ),
    'Variation' => array(
        (int) 0 => array(
            'title' => 'Short',
        ),
        (int) 1 => array(
            'title' => 'Long',
        )
    )
),
)

非常感谢任何帮助。

编辑:

我设法像这样解决它

$res=[];

foreach($test as $row){
    $res[$row['Attribute']['title']][]=$row['Variation']['title'];
}

给了我这个

array(
'Size' => array(
    (int) 0 => '40 inch',
    (int) 1 => '42 inch',
),
'Length' => array(
    (int) 0 => 'Long',
    (int) 1 => 'Short',
),
)

这就是我可以使用的东西。 感谢大家的投入。 我赞成所有的答案,但因为我只是一个新手,它没有公开展示。

2 个答案:

答案 0 :(得分:0)

虽然主阵列的index不同,但他可以提供帮助 -

$new= array();

foreach($var as $arr) {

    $new[$arr['Attribute']['title']]['Attribute']['title'] = $arr['Attribute']['title'];
    $new[$arr['Attribute']['title']]['Variation'][] = array('title' => $arr['Variation']['title']); 

}

Demo

答案 1 :(得分:0)

<?php
$old = array(
    (int) 0 => array(
        'Variation' => array(
            'title' => '40 inch'
        ),
        'Attribute' => array(
            'title' => 'Size'
        )
    ),
    (int) 1 => array(
        'Variation' => array(
            'title' => '42 inch'
        ),
        'Attribute' => array(
            'title' => 'Size'
        )
    ),
    (int) 2 => array(
        'Variation' => array(
            'title' => 'Long'
        ),
        'Attribute' => array(
            'title' => 'Length'
        )
    ),
    (int) 3 => array(
        'Variation' => array(
            'title' => 'Short'
        ),
        'Attribute' => array(
            'title' => 'Length'
        )
    )
);
$new = array();
$attr = '';
$i = -1;
foreach ($old as $arr) {
    if($attr != $arr['Attribute']['title']){
        $attr = $arr['Attribute']['title'];
        $i++;
    }
    $new[$i]['Attribute']['title'] = $arr['Attribute']['title'];
    $new[$i]['Variation'][] = array('title' => $arr['Variation']['title']);
}
print_r($new);
?>

输出是:

Array
(
    [0] => Array
        (
            [Attribute] => Array
                (
                    [title] => Size
                )

            [Variation] => Array
                (
                    [0] => Array
                        (
                            [title] => 40 inch
                        )

                    [1] => Array
                        (
                            [title] => 42 inch
                        )

                )

        )

    [1] => Array
        (
            [Attribute] => Array
                (
                    [title] => Length
                )

            [Variation] => Array
                (
                    [0] => Array
                        (
                            [title] => Long
                        )

                    [1] => Array
                        (
                            [title] => Short
                        )

                )

        )

)