如何根据相同类型的键对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.

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

Both type are not the same

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

code for group them

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