Question: I have an array of strings of the format "Year-Month\_ID". I'd like to split each item in two at the '\_'. Then, for the items that have the same prefix (year & month), join the IDs with '\_' after the prefix ("Year-Month\_ID\_ID..."). For example, if I have the data:

```
array(
    '2010-December_9',
    '2010-December_19',
    '2011-June_4',
    '2011-September_38',
    '2011-November_29',
    '2011-November_9'
)

```

I want the result to be:

```
array(
    '2010-December_9_19',
    '2011-June_4',
    '2011-September_38',
    '2011-November_29_9'
)

```

How can I do this?

Answer: 
```
$arr = array();
foreach ($dates as $val)
{   
    $tmp = explode("_", $val);
    if ( ! isset($arr[$tmp[0]]))
    {   
        $arr[$tmp[0]] = array();
    }   
    $arr[$tmp[0]][] = $tmp[1];
}   
$final = array();
foreach ($arr as $key=>$val)
{   
    $final[] = $key."_".implode("_", $val);
}

```
This might help:

```
$arr = Array('2010-December_9', '2010-December_19', '2011-June_4', '2011-September_38', '2011-November_29', '2011-November_9'); // input array
$arrTemp = $arrOutput = Array();
foreach ($arr as $val) {
    if (preg_match('/([0-9]{4})-([a-z]+)_([0-9]{1,2})/i', $val, $arrMatches)) {
        $arrTemp[$arrMatches[1] . '-' . $arrMatches[2]][] = $arrMatches[3];
    }
}
foreach ($arrTemp as $key => $day) {
    $arrOutput[] = $key . '_' . implode('_', $day);
}
print_r($arrOutput);

```