0

PHP Noob here, in over my head.

I have a file listing script that's checking through a directory and producing a series of file paths. They look more or less like this:

2014/1Q/ES/PDFs/141QES_01.pdf

I want to iterate through the list (it's a long list) and construct a nested associative array or object from which I can create an organized file tree with download links. The catch is I don't want my method to be specific to the path structure for this particular site, I want to build it dynamically based on the directory structure in the file paths so I can use this on other sites with similar needs.

$results = array(
    '2014' => array(
        '1Q' => array(
            'ES' => array(
                'PDFs' => array(
                    '141QES_01.pdf',
                    '141QES_02.pdf',
                    ...
                )
            ),
            'SE' => array(
                'PDFs' => array(
                    '141QSE_01.pdf',
                    '141QSE_02.pdf',
                    ...
                )
            ),
            ...
        ),
        '2Q' => array(
            'ES' => array(
                'PDFs' => array(
                    '141QES_01.pdf',
                    '141QES_02.pdf',
                    ...
                )
            ),
            'SE' => array(
                'PDFs' => array(
                    '141QSE_01.pdf',
                    '141QSE_02.pdf',
                    ...
                )
            ),
            ...
        )...
    ),
    '2015' => array(
        ...
    )
)

What's killing me is I can explode the paths into an array that contains the exact keys and filename I need, but I can't for the life of me figure out how to inject that flat array into the larger associative array. I'm betting it's a lot simpler than I'm making it, so I could use some wisdom. If I'm being stupid, I don't mind hearing it.

Mr Eazwah
  • 11
  • 2
  • You just need to use an iterator, what have you tried? – Devon Bessemer May 22 '15 at 01:55
  • http://stackoverflow.com/questions/4050511/how-to-list-files-and-folder-in-a-dir-php – Axalix May 22 '15 at 01:55
  • Devon: the entire list of paths is in an array so I can foreach through the whole thing, but what I can't work out is how to inject the keys/filename from each path into the associative array and keep the nesting structure how I want it. – Mr Eazwah May 22 '15 at 02:04
  • Axalix: Thx for the link I'll look through that to see if this is covered. – Mr Eazwah May 22 '15 at 02:05
  • Axalix, I think you've pointed me in the right direction. Looks like I should be doing this as the paths are generated, not after the fact. – Mr Eazwah May 22 '15 at 02:09

1 Answers1

0

Maybe I'm misunderstanding something here but here's how you'd add a value to the PDF's array for the file 2014/1Q/ES/PDFs/141QES_03.pdf

$results = array(
        '2014' => array(
            '1Q' => array(
                'ES' => array(
                    'PDFs' => array(
                        '141QES_01.pdf',
                        '141QES_02.pdf'

                    )
                ),
                'SE' => array(
                    'PDFs' => array(
                        '141QSE_01.pdf',
                        '141QSE_02.pdf',

                    )
                )

            ),
            '2Q' => array(
                'ES' => array(
                    'PDFs' => array(
                        '141QES_01.pdf',
                        '141QES_02.pdf',

                    )
                ),
                'SE' => array(
                    'PDFs' => array(
                        '141QSE_01.pdf',
                        '141QSE_02.pdf',

                    )
                )

            )
        ));

Insertion code:

 //insert value
    $tmp = '2014/1Q/ES/PDFs/141QES_03.pdf';
    $tmpArr = explode('/',$tmp);
    $results[$tmpArr[0]][$tmpArr[1]][$tmpArr[2]][$tmpArr[3]][]=$tmpArr[4];

    var_dump($results);

This outputs:

array(1) {
  [2014]=>
  array(2) {
    ["1Q"]=>
    array(2) {
      ["ES"]=>
      array(1) {
        ["PDFs"]=>
        array(3) {
          [0]=>
          string(13) "141QES_01.pdf"
          [1]=>
          string(13) "141QES_02.pdf"
          [2]=>
          string(13) "141QES_03.pdf"
        }
      }...
eol
  • 23,236
  • 5
  • 46
  • 64
  • $results[$tmpArr[0]][$tmpArr[1]][$tmpArr[2]][$tmpArr[3]][]=$tmpArr[4]; Thats the part I don't wanna do. I want these keys to be generated dynamically based on the results of the path explode. Not every path will be 4 levels deep. – Mr Eazwah May 22 '15 at 02:17