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

时间:2014-04-19 04:19:33

标签: php arrays

我正在尝试获取数组的一部分,但是当我执行时,密钥不会随身携带。

例如,如果我有:

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

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

我想得到索引为default_route的数组,它返回该数组,但键被删除,结果是:

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

我尝试了array_intersect_key($routes, array_flip(array($key)));,但是将一个数组放入另一个数组中,这是毫无意义的,因为一次不会有多个数组。我不想要一个包含一个元素的二维数组,我只想要具有正确键值的数组。

实现:

    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;

这是路由的构造函数。我只是拿着阵列把它分开。

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

我只想拉出该部分并保留关键字符串。我觉得这很容易,我只是遗漏了一些东西。

2 个答案:

答案 0 :(得分:0)

我认为问题在于你是如何构建$newRouteObjs的,因为你传入了[]这是一个未编入索引的数组。只需将其编入索引:

foreach ($routes as $key => $val)
{ 
    $routeObj = new Route($routes[$key]);

    //add [$key] as opposed to []
    $newRouteObjs[$key] = $routeObj;
}

return $newRouteObjs;

这就是转储:

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

    [hello_route] => Route Object
    (
        [name] => path
        [path] => /hello
        [controller] => HelloController
        [action] => helloAction
    )

)

And here's a working example

答案 1 :(得分:0)

为什么不简单地在构造函数中为名称添加新参数?

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

然后你可以把它称为:

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

它应该给你想要的结果:

Array
(
    [0] => Route Object
        (
            [name] => default_route
            [path] => /
            [controller] => IndexController
            [action] => indexAction
        )

    [1] => Route Object
        (
            [name] => hello_route
            [path] => /hello
            [controller] => HelloController
            [action] => helloAction
        )

)

您可以使用一种方法来绕过丢失密钥的限制,将其重新分配到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;
}

上课:

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