I'm trying to build an array structure to get the data easier. Maybe somebody can help me?
how can I insert the value as key value from another array into it?
$pers = [
'2019/Herbert',
'2019/Wolfgang',
'2020/Doris',
'2020/Musti',
];
multidimensional array representing filepath
function parse_paths_of_files($array) {
rsort($array);
$result = array();
foreach ($array as $item) {
$parts = explode('/', $item);
$current = &$result;
include 'parentdir/'.$item.'/data.php';
//echo $article['date']; // example: 2020-05-06
for ($i = 1, $max = count($parts); $i < $max; $i++) {
if (!isset($current[$parts[$i - 1]])) {
$current[$parts[$i - 1]] = array();
}
$current = &$current[$parts[$i - 1]];
}
$last = end($parts);
if (!isset($current[$last]) && $last) {
// Don't add a folder name as an element if that folder has items
$current[] = end($parts);
}
}
return $result;
}
echo '<pre>'; print_r(parse_paths_of_files($pers)); echo '</pre>';
The result with automatic indexing:
Array
( // by rsort($array);
[2020] => Array
(
[0] => Herbert
[1] => Wolfgang
)
[2019] => Array
(
[0] => Doris
[1] => Musti
)
)
I would like to use the included date (2020-05-06) as a key:
Array
(
// by rsort($array);
[2020] => Array
( // by rsort(???);
[2020-12-06] => Herbert
[2020-10-09] => Wolfgang
[2020-05-19] => Andy
)
[2019] => Array
(
[2019-12-22] => Doris
[2019-10-02] => Musti
[2019-01-21] => Alex
[2019-01-20] => Felix
)
)
Thanks for your answers, since I am a beginner and do not really understand everything, it is not easy for me to formulate or prepare the questions correctly. Sort for that! Greetings from Vienna!