dqybeh2884 2014-11-03 12:58
浏览 81
已采纳

如何根据相同类型的键对Array进行分组

what i need

  • I Need to group array of same type.
  • like array1 ['type']=a and array2 ['type']=a then it should be grouped in single array.

array structure

 Array
(
[1] => Array
(
    [type] => ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular
    [amount] => 850
    [comment] => On or Before June 28, 2014
)

[2] => Array
(
    [type] => ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Friend
    [amount] => 1000
    [comment] => On or Before June 28, 2014
)

[3] => Array
(
    [type] => ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Student
    [amount] => 500
    [comment] => On or Before June 28, 2014
)

[4] => Array
(
    [type] => ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular)
    [amount] => 990
    [comment] => June 29 thru July 20, 2014
)

PHP Code

foreach($data as $k=>$v){
    $type[$v['type']][]=$k;
}

//loop types, creating result array
foreach($type as $k=>$v){
    $tmp=array(
    'type'=>$kk,
    'metadata'=>array()
);

//loop all the arrays of this type
foreach($v as $w){
    //store in TMP
    $t=array(
        'amount' => $data[$w]['amount'],
        'comment' => $data[$w]['comment']
    );
    //store 
    $tmp['metadata'][]=$t;
}

$result[]=$tmp;

}

output of type

[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular] => Array
(
    [0] => 1
    [1] => 7
)

[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Friend] => Array
(
    [0] => 2
    [1] => 5
    [2] => 8
)

[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Student] => Array
(
    [0] => 3
    [1] => 6
    [2] => 9
)

[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular)] => Array
(
    [0] => 4
)

Problem im getting

[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular] => Array
(
    [0] => 1
    [1] => 7
)

[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular)] => Array
(
    [0] => 4
)
  • this array having same type but is not grouped.

  • i want ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular to grouped in single array .

  • how to tackle this problem any suggestion are most welcome.

  • 写回答

2条回答 默认 最新

  • dongtan7201 2014-11-03 13:04
    关注

    Most likely because of that superfluous ) on that other value:

    ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular
    ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular)
    // extra `)` on the end
    

    Alternatively, you could simply fix it by trimming it:

    foreach($data as $k => $v){
        $type_val = rtrim($v['type'], ')');
        $type[$type_val][] = $k;
    }
    

    You could push them and group them like this:

    $new_data = array();
    foreach($data as $k => $v){
        $type_val = rtrim($v['type'], ')');
        // simple initialize
        if(!isset($new_data[$type_val])) {
            $new_data[$type_val] = array(
                'amount' => 0,
                'comment' => array(),
            );
        }
    
        // push values
        $new_data[$type_val]['amount'] += $v['amount'];
        $new_data[$type_val]['comment'][] = $v['comment'] . "
    ";
    }
    
    echo '<pre>';
    print_r($new_data);
    

    Simple Output of the snippet above

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法