Hey there
I just started out with PHP so it might be a trivial problem. I want to populate an array with values by a recursive function but i don't get it the way i want it to have.
Target array (written in python syntax):
myArray = {
key : [
[a1, a2, a3, a4],
[b1, b2, b3 ,b4]
],
key2 : [
[c1, c2, c3, c4],
[d1, d2, d3, d4]
]
}
So far i created a recursive function to collect all data i need for the array but i don't get the right syntax to achieve my target array.
What i have so far is following code (snippet):
function get_data($main_dir, $items=[], $myArray=[], $depth=0) {
global $items;
if ($myArray == NULL && depth == 0) {
$myArray = array();
}
$depth++;
$dirHandle = opendir($main_dir);
while ($file = readdir($dirHandle)){
if(is_dir($main_dir.$file) && $file != "." && $file != "..") {
$new_dir = $main_dir.$file."/";
$items = get_data($new_dir, $items, $myArray, $depth);
$myArray = $items[1]
$items = $items[0]
}
else {
splitted_dir = preg_split("/\//", $main_dir);
key = splitted_dir[2];
file_information = read_out_file($main_dir.$file);
information_1 = file_information[0];
information_2 = file_information[1];
myArray = array_merge($myArray, array($key, array($key, $information_1, $information_2, $main_dir, $file))
}
}
}
The array contains all informations i need but the structure of it is not useable.
Current output:
Array (
[0] => key1
[1] => Array (
[0] => key1
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
[2] => key1
[3] => Array (
[0] => key1
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
[4] => key1
[5] => Array (
[0] => key1
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
[6] => key2
[7] => Array (
[0] => key2
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
[8] => key2
[9] => Array (
[0] => key2
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
) 1
## IDK why this "1" at the end gets printed out...
But what i want to get is this:
Array (
[key1] => Array (
Array (
[0] => key1
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
)
Array (
[0] => key1
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
)
)
[key2] => Array (
Array (
[0] => key2
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
)
Array (
[0] => key2
[1] => information_1
[2] => information_2
[3] => ./path/
[4] => filename )
)
)
)
At the end i want to sort the output by alphabetic order of the keys which i should achieve with sort() i guess?
Greetings