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: 
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);

```
`explode('_',$str)`

<http://php.net/manual/en/function.explode.php>

I will put down a simple logic .. implement it with code.

```
create array2

for loop till all elem in array1
  loop2 for array1
    if index1!=index2 && explode('_',array[index1])[0]== explode('_',array[index2])[0]
         array2.push( explode('_',array[index1])[0]).explode('_',array[index1])[1]).'_'.explode('_',array[index2])[1])
          array1.pop(index1)
          array1.pop(index2)
  loop end
loop end

for loop till all rem elem in array1
  array2.push(array1[index])
 loop end

```