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

时间:2014-11-03 12:58:03

标签: php arrays

我需要什么

  • 我需要对相同类型的数组进行分组。
  • array1 ['type']=aarray2 ['type']=a类似,然后应将其分组为单个数组。

数组结构

 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代码

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;

}

输出类型

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

问题即将到来

[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
)
  • 此数组具有相同类型但未分组。

  • 我希望ECOOP会议,研讨会和UPMARC暑期学校(周一至周五7月28日 - 8月1日)定期分组。

  • 如何解决这个问题,欢迎提出任何建议。

2 个答案:

答案 0 :(得分:1)

很可能是因为那个多余的)就是另一个值:

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

或者,您可以通过修剪它来简单地修复它:

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

你可以推动它们并将它们分组:

$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'] . "\n";
}

echo '<pre>';
print_r($new_data);
上面代码段的

Simple Output

答案 1 :(得分:0)

两种类型都不一样

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

将它们分组的代码

$newArr = array();
foreach($array as $key=>$value) {
    $newArr[$value['type']][] = $value; 
}