1

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!

Cem Firat
  • 390
  • 2
  • 21

1 Answers1

1

Assuming that $artilce['date'] is defined in the included file, this builds the proper structure. You might want to check for the date and if not there set to some other value. Also, keep in mind that if more than one article has the same date then only the one appearing last in the $pers array will be in the result:

function parse_paths_of_files($paths, &$array=array()) {
    foreach($paths as $path) {
        include "parentdir/$path/data.php";
        $path = explode('/', $path);
        $value = array_pop($path);

        $temp =& $array;

        foreach($path as $key) {
            $temp =& $temp[$key];
        }
        $temp[$article['date']] = $value;
    }
}

parse_paths_of_files($pers, $result);

Built off of my answer to something similar How to access and manipulate multi-dimensional array by key names / path?.

After all of that, with the same caveats, I think this change in your existing code would work:

$current[$article['date']] = end($parts);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Hello @AbraCadaver, thanks for your answer! I have replaced the ** above code ** with mine, I should have done something wrong - I get a blank page. But if I only exchange the << $ current [$ article ['date']] = end ($ parts); >> with the existing << $ current [] = end ($ parts); >> code then it works ! Thank you! How can I control the sorting? Sort by date? I tried to insert the code with different $ vars << rsort ($ var); >> here and there, but unfortunately it doesn't work. – Cem Firat May 14 '20 at 06:36