I have this issue where I am trying to add values from one array to another that's nested under the original array's name.
For example, this script returns an array:
<?php
return array (
'HTTP' => array(
'GET' => array(
'/' => function() {
},
),
'POST' => array(
'/' => function($username, $password) {
},
)
),
'HTTPS' => array(
'GET' => array(
'/account/(:num)' => function($number) {
}
)
)
Then I have a class with a var of:
private $routes = array(
'HTTP' => array(
'GET' => array(),
'POST' => array()
),
'HTTPS' => array(
'GET' => array(),
'POST' => array()
),
'FILTRERS' =>
'BEFORE' => array(),
'AFTER' => array()
);
What I am doing is using glob
to read all the files, but the issue I can't work out is how to loop though all the arrays from the first scrsipt and push them into the var.
var would be:
private $routes = array(
'HTTP' => array(
'GET' => array(
'example' => array(
'/' => function() {
},
)
),
'POST' => array(
'example' => array(
'/account/(:num)' => function($number) {
},
)
)
),
'HTTPS' => array(
'GET' => array(
'example' => array(
'/' => function() {
},
)
),
'POST' => array()
),
'FILTRERS' =>
'BEFORE' => array(),
'AFTER' => array()
);
The only why I can think of is using around four nested foreach
es, or duplicating the code for each array set
Assuming the first array is stored in $input
you can use this. I have hard-coded 'example'
based on your expected output.
foreach ($input as $protocol => $methods) {
foreach ($methods as $method => $routeArray) {
$routes[$protocol][$method]['example'] = $routeArray;
}
}