合并数组项特定的数组项

I got this array:

array (
  0 => 'K.',
  1 => 'Vrachtschip',
  2 => 'L.',
  3 => 'Gevechtsschip',
  4 => 'Z.',
  5 => 'Gevechtsschip',
  6 => 'Kruiser',
  7 => 'Slagschip',
  8 => 'Bommenwerper',
  9 => 'Vernietiger',
  10 => 'Interceptor.',
)

of can I merge the items [0] with [1], because K. vrachtschip must be together. same ass [2] and [3]; and [4] with [5]. if there is 1 letter and then a dot (k.) it must be merged with the following array item.

Anyone that can help me :)?

How about:

$arr = array ( 
    'K.',
    'Vrachtschip',
    'L.',
    'Gevechtsschip',
    'Z.',
    'Gevechtsschip',
    'Kruiser',
    'Slagschip',
    'Bommenwerper',
    'Vernietiger',
    'Interceptor',
    'B.',
);

$concat = '';
$result = array();
foreach ($arr as $elem) {
    if (preg_match('/^[A-Z]\.$/', $elem)) {
        $concat = $elem;
        continue;
    }
    $result[] = $concat.$elem;
    $concat = '';
}
if ($concat) $result[] = $concat;
print_r($result);

output:

Array
(
    [0] => K.Vrachtschip
    [1] => L.Gevechtsschip
    [2] => Z.Gevechtsschip
    [3] => Kruiser
    [4] => Slagschip
    [5] => Bommenwerper
    [6] => Vernietiger
    [7] => Interceptor
    [8] => B.
)

Try to use a regular expression to test all entries of your array.
If an occurence is founded, concat the value of your entrie with the next.

I'd probably do the following (pseudo code):

  1. Create empty array for result
  2. Iterate the original array
  3. For each value: does it match /^[a-z]\.$/i?
  4. If yes, see if original array contains a next element?
  5. If yes, concatenate the two items and add to resulting array, skip next item in loop
  6. If no (pt. 4 or 5) add directly to resulting array.

I would try something like this:

for($idx=0, $max = count($array_in); $idx < $max; $idx++)
{
   if(preg_match('/^[a-z]\.$/i', $array_in[$idx]))
   {
      $array_out[] = $array_in[$idx].$array_in[$idx+1];
      $idx++;
   }
   else
   {
      $array_out[] = $array_in[$idx];
   }

}