-1

我有一个数组:

Array
    (
    [0] => Array
        (
            [id] => 0
            [type] => type1
            [aa] => 1.00
            [bb] => 0.00
            [cc] => 1.00
            [group] => new
        ),
    [1] => Array
        (
            [id] => 1
            [type] => type2
            [aa] => 1.00
            [bb] => 0.00
            [cc] => 1.00
            [group] => new
        ),
    [2] => Array
        (
            [id] => 4521081
            [type] => type3
            [aa] => 1.00
            [bb] => 0.00
            [cc] => 1.00
            [group] => old
        )
);

如何对上述数组基键进行分组检查。结果看起来像这样:

Array(
    'new'=>array(
        'type1'=>array(
                    [id] => 0
                    [aa] => 1.00
                    [bb] => 0.00
                    [cc] => 1.00
                    ),
        'type2'=>array(
                        [aa] => 1.00
                        [bb] => 0.00
                        [cc] => 1.00
                        )
    ),
    'old'=>array(
             'type3'=>array(
                        [aa] => 1.00
                        [bb] => 0.00
                        [cc] => 1.00
                        )
            )
);
4

3 回答 3

1

试试这个 :

$res    = array();
foreach($your_array as $val){
   $res[$val['group']][$val['type']]['id']  = $val['id'];
   $res[$val['group']][$val['type']]['aa']  = $val['aa'];
   $res[$val['group']][$val['type']]['bb']  = $val['bb'];
   $res[$val['group']][$val['type']]['cc']  = $val['cc'];
}

echo "<pre>";
print_r($res);
于 2013-03-22T06:47:05.310 回答
0
foreach( $array as $each )
{
   $tmp = $each;
   unset( $tmp['group'],$tmp['type'] );
   $output[$each['group']][$each['type']] = $tmp;
}
于 2013-03-22T06:50:56.500 回答
0

我愿意:

foreach($array as $each)
{
  $output[$each['group']] = $each;
}

它有更大的尺寸,但看起来不错,有时您也需要组。

于 2013-03-22T06:56:16.280 回答