tags:

views:

89

answers:

1

Hello All, I would like a hint or much better a solution for this:

I do a regular expresion match to an url for example '/product/100/'

preg_match('/^\/(?<name>\w+)\/(?<digit>\d+)\/$/', '/product/100/', $matches);

As result of this I get the following array on $matches:

array
  0 => string '/product/100/' (length=13)
  'name' => string 'product' (length=7)
  1 => string 'product' (length=7)
  'digit' => string '100' (length=3)
  2 => string '100' (length=3)

How can I use reduce this array to get this?

array
  'name' => string 'product' (length=7)
  'digit' => string '100' (length=3)

After I get the matching expresions, I call a function and give the 'named' keys as arguments to the function.

call_user_func_array($view, $just_named_args_no_integer_keys);

I hope anyone can give me any hint.

Best Regards

+1  A: 

Just run the keys you get from array_keys() through array_filter():

/* This is for PHP 5.3, I'm sure you'll figure out how to the same thing pre 5.3 :) */

$allKeys = array_keys($view);
$namedKeys = array_filter($allKeys, function($value) { return !is_numeric($value); });

Update

Did not read the question properly. In this case, actually just foreach over the data:

$namedValues = array();
foreach ($view as $key => $value)
    if (!is_numeric($key))
        $namedValues[$key] = $value;
MathieuK
ok, I get the keys but miss the values ...
Mario César
Pre-5.3, the second argument is: `create_function('$value', 'return !is_numeric($value);')`
strager
@Mario: see my update, sorry :)
MathieuK
@strager sure, although I would strongly suggest, for maintenance reasons to just create an actual function.
MathieuK
Thanks, it work just great, simple :)
Mario César