1

I've been struggling quite a bit with this. I have a folder with a list of files and a basic naming order - abc-def-xyz.php

I'm trying to create an index page to list all of the files automatically in groups. So:

abc-def-xyz.php
abc-def-xzz.php
abb-def-xyz.php

Would show in the php index page as:

abc
   def
      <a href="link/to/file">xyz</a>
      <a href="link/to/file">xzz</a>
abb
   def
      <a href="link/to/file">xyz</a>

I can get to the point of exploding the file names and removing the extension, then i'm just lost. Any advice would be much appreciated. Thank you!

Array
(
    [0] => Array (this is applyOnline-alert-warning.php)
    (
        [0] => applyOnline
        [1] => alert
        [2] => warning
    )

    [1] => Array
    (
        [0] => applyOnline
        [1] => collectionOrDelivery
    )
)
JP Schutte
  • 11
  • 3
  • Not sure why you'd bother with a multidimensional array. I'd sort the set, and just compare each file with the one before it. If the last section changes but the first and second don't, write a new entry. If the second section changes but the first doesn't, write a new subheader and then a new entry. If the sirst section changes, write a new header, subheader, and entry. Always close individual entries. Close subheaders and headers when you write a new one. Watch you start and finish logic. – Paul Hodges Oct 04 '17 at 19:58
  • Your example array is inconsistent. The 2nd element only has 2 items in it. Where is the 3rd? – gview Oct 04 '17 at 20:03
  • Did you give up??? – AbraCadaver Oct 12 '17 at 17:14

1 Answers1

1

Using the methodology from this answer How to write getter/setter to access multi-level array by key names?:

$array = array();

foreach($files as $file) {
    $temp = &$array;
    foreach(explode('-', $file) as $key) {
        $temp =& $temp[pathinfo($key, PATHINFO_FILENAME)];
    }
    $temp = $file;
}

Yields an array where the key names can be used:

Array
(
    [abc] => Array
        (
            [def] => Array
                (
                    [xyz] => abc-def-xyz.php
                    [xzz] => abc-def-xzz.php
                )

        )

    [abb] => Array
        (
            [def] => Array
                (
                    [xyz] => abb-def-xyz.php
                )

        )

)
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87