I have a regex pattern that will match some elements from a string and give them a particular name. For example, `#^(?<foo>.*)$#` will match the whole string and name it `foo`.

My problem is that the matches also contain the "classic", numbered matches.

For example:

```
<?php

$pattern = '#^(?<foo>.*)$#';
$str = '123';
$matches = null;
preg_match($pattern, $str, $matches);
print_r($matches);

```

will print:

```
Array
(
    [0] => 123
    [foo] => 123
    [1] => 123
)

```

As all the matches will always be named, I decided to manually remove the numbered indexes from `$matches` in order to clean things up:

```
<?php

$pattern = '#^(?<foo>.*)$#';
$str = '123';
$matches = null;
if (preg_match($pattern, $str, $matches))
{
    foreach ($matches as $key => $value)
    {
        if (is_int($key))
            unset($matches[$key]);
    }
}
print_r($matches);

```

Which prints:

```
Array
(
    [foo] => 123
)

```

It works, but I feel that it can be improved. Is there a better way to do this, especially without the `foreach` loop?

In practice, `$pattern` and `$str` can be much more complicated than the example I gave and I want this to be executed as fast as possible.