0

私は配列を持っています:

Array
(
    [0] => Array
        (
            [attribute_name] => Appliances
            [attribute_value] => Washer
        )

    [1] => Array
        (
            [attribute_name] => Appliances
            [attribute_value] => Dryer
        )

    [2] => Array
        (
            [attribute_name] => Appliances
            [attribute_value] => Dishwasher
        )

    [3] => Array
        (
            [attribute_name] => Appliances
            [attribute_value] => Microwave
        )

    [4] => Array
        (
            [attribute_name] => Console
            [attribute_value] => Xbox360
        )

    [5] => Array
        (
            [attribute_name] => Console
            [attribute_value] => PS3
        )
)

私は生産したい:

Array
(
    [0] => Array
        (
            [attribute_name] => Appliances
            [attribute_value] => Washer, Dryer, Dishwasher, Microwave
        )

    [1] => Array
        (
            [attribute_name] => Console
            [attribute_value] => Xbox360, PS3
        )
)

これはPHPでどのように達成されますか?

@andrewtweber の元のソリューションに基づく最終的なコードは次のとおりです。

http://codepad.org/E4WFnkbc

4

1 に答える 1

4
$new_arr = array();

foreach( $arr as $data ) {
    if( !isset($new_arr[$data['attribute_name']]) ) {
        $new_arr[$data['attribute_name']] = array();
    }
    $new_arr[$data['attribute_name']][] = $data['attribute_value'];
}

これにより、

array( 'Appliances' => array( 'Washer', 'Dryer', 'Dishwasher' ) );

http://codepad.org/m6l3je0H

于 2012-06-27T23:40:45.320 に答える