0

I have an array of strings:

$routes = Array
(
    [0] => Array
        (
            [0] => /
        )

    [1] => Array
        (
            [0] => /articles/[:slug]?
        )

    [2] => Array
        (
            [0] => /articles/[:cid]/[:slug]?
        )

    [3] => Array
        (
            [0] => /articles/[a:year]/?[*:month]?/?[*:day]?
        )
)

And a data array with params below. Based on this data I want to find best match from routes.

Array
(
    [year] => 2012
    [day] => 11
    [month] => 01
)

In the example above: I want to get $routes[3].

I have tried something like this:

foreach($routes as $route) {
            if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route[0], $matches, PREG_SET_ORDER)) {

                foreach($matches as $match) {
                    list($block, $pre, $type, $param, $optional) = $match;
                    // How to check???
                }
            }
}
4
  • 3
    1) Have you tried something to get to your goal? 2) Why is element 3 the "best match" ? Commented Dec 10, 2015 at 1:16
  • I have tried something but unsuccessfully. Commented Dec 10, 2015 at 1:19
  • 2
    You should include that to help potential answers. Commented Dec 10, 2015 at 1:28
  • 1
    Please check my updated question with code I have tried Commented Dec 10, 2015 at 2:15

2 Answers 2

1

Assuming you want:

// $bestRoute = to the content of $routes[3]
$bestRoute = pathfinderFunc($routes, array ([year] => '2012', [day] => '11', [month] => '01' ));

The following function takes the $routes array and an associative array like your example. It tries to match all of the keys of the associative array to the strings in $routes. If a match is found, it returns the contents of the array that contains the matched route. If no match is found, it returns false.

function pathfinderFunc($routes, $match) {
  $keys = array_keys($match);
  $isMatch = false;
  foreach($routes as $route) {
    foreach($keys as $key) {
      if(strpos($route[0], $key) === false)
        continue 2;
    }
    return $route;
  }
  return false; // no good match found
}
Sign up to request clarification or add additional context in comments.

Comments

0

I believe he wants to get which route number from route params.

And this is just an idea depend on how will you go.

I will define more details on my route array as below to make things more precise, you can check from number of parameter or whatever.

$routes = array(
[0] => Array
    (
        [pattern] => /
     [param_num] =>0
        [params] =>array()
    )

[1] => Array
    (
        [pattern] => /articles/[:slug]?
        [param_num] =>1
        [params] => array()
    )

[2] => Array
    (
        [pattern] => /articles/[:cid]/[:slug]?
        [param_num] =>2
        [params] => array()
    )

[3] => Array
    (
        [0] => /articles/[a:year]/?[*:month]?/?[*:day]?
        [param_num] =>3
        [params] => array('year', 'month', 'day')
    ));

/*
Array
(
    [year] => 2012
    [day] => 11
    [month] => 01
)
*/
$param_count =  count($params);
$select_route = null;
foreach( $routes as $route){

 if( $route['param_num'] == $param_count){
    $select_route = $route;
    break;
  }

}

This is just example , you may have some way to use params details of each route detail to check something.

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.