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.
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'];
}
}
I think the issue is just how you construct $newRouteObjs
because you're passing in []
which is an unindexed array. Simply index it:
foreach ($routes as $key => $val)
{
$routeObj = new Route($routes[$key]);
//add [$key] as opposed to []
$newRouteObjs[$key] = $routeObj;
}
return $newRouteObjs;
And this is the dump:
Array
(
[default_route] => Route Object
(
[name] => path
[path] => /
[controller] => IndexController
[action] => indexAction
)
[hello_route] => Route Object
(
[name] => path
[path] => /hello
[controller] => HelloController
[action] => helloAction
)
)