dpdjv9559 2014-04-19 04:19
浏览 17
已采纳

获取数组的一部分并保持键值

I am trying to get a part of an array but when I do the key does not carry over with it.

for instance if I have:

Array
(
    [default_route] => Array
        (
            [path] => /
            [controller] => IndexController
            [action] => indexAction
        )

    [hello_route] => Array
        (
            [path] => /hello
            [controller] => HelloController
            [action] => helloAction
        )
)

and I would like to get just the array with the index of default_route it returns that array but the key is removed so the result is:

Array <- no more key string...
(
    [path] => /
    [controller] => IndexController
    [action] => indexAction
)

I tried array_intersect_key($routes, array_flip(array($key))); but that places a single array into another array which is pointless because there will never be more than one at a time. I don't want a 2 dimensional array with one element in it, I would like just the array with the correct key value.

implementation:

    foreach ($routes as $key => $val)
    {
        // $routeArray = array_intersect_key($routes, array_flip(array($key)));
        // would put whats above but that is what creates the unnecessary two  dimensional array 
        $routeObj = new Route($routes[$key]);

        $newRouteObjs[] = $routeObj;
    }

    return $newRouteObjs;

and here is the constructor for the route. I was just going to take the array and break it apart.

public function __construct(array $route)
{
    $this->name = key($route);
    $this->path = $route['path'];
    $this->controller = $route['controller'];
    $this->action = $route['action'];
}

I simply want to pull out the section and keep the key string. I have a feeling this is easy and I am just missing something.

  • 写回答

2条回答 默认 最新

  • doupai1876 2014-04-19 04:49
    关注

    Why don't you simple add a new parameter to your constructor for the name?

    class Route
    {
        public function __construct(array $route, $name)
        {
            $this->name = $name;
            $this->path = $route['path'];
            $this->controller = $route['controller'];
            $this->action = $route['action'];
        }
    }
    

    Then you can call it as:

    $routeObj = new Route($routes[$key], $key);
    

    And it should give you the desired result as:

    Array
    (
        [0] => Route Object
            (
                [name] => default_route
                [path] => /
                [controller] => IndexController
                [action] => indexAction
            )
    
        [1] => Route Object
            (
                [name] => hello_route
                [path] => /hello
                [controller] => HelloController
                [action] => helloAction
            )
    
    )
    

    And one way you could use to bypass the limitation of losing the key would be to re-assign it within your foreach:

    foreach ($routes as $key => $val)
    {
        // $routeArray = array_intersect_key($routes, array_flip(array($key)));
        // would put whats above but that is what creates the unnecessary two  dimensional array 
        $routes[$key]['name'] = $key;
        $routeObj = new Route($routes[$key]);
    
        $newRouteObjs[] = $routeObj;
    }
    

    And the class:

    class Route
    {
        public function __construct(array $route)
        {
            $this->name = $route['name'];
            $this->path = $route['path'];
            $this->controller = $route['controller'];
            $this->action = $route['action'];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?