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!

5
  • What all does $parts have? Commented May 13, 2020 at 15:31
  • @vivek_23 parts example: 2019, Herbert Commented May 13, 2020 at 15:52
  • Ok, it's difficult to say anything. How are you getting those dates? Commented May 13, 2020 at 17:39
  • @vivek_23 I assumed it was in the include include 'parentdir/'.$item.'/data.php'; //echo $article['date']; // example: 2020-05-06 Commented May 13, 2020 at 18:10
  • @AbraCadaver Yes, seems so after I read it again. Commented May 13, 2020 at 20:53

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.