katie hudson
katie hudson

Reputation: 2893

Returning folder structure in alphabetical order

I am trying to understand something. I have the following function

function fillArrayWithFileNodes( DirectoryIterator $dir )
{
    $data = array();
    foreach ( $dir as $node )
    {
        if ( $node->isDir() && !$node->isDot()  )
        {
            $data[$node->getFilename()] = $this->fillArrayWithFileNodes( new DirectoryIterator( $node->getPathname() ) );
        }
        else if ( $node->isFile() )
        {
            $data[] = $node->getFilename();
        }
    }
    return $data;
}

I essentially pass it a root path and in creates an array with the directory structure like so.

$fileData = $this->fillArrayWithFileNodes(new DirectoryIterator( public_path(). '/images'));

The output might be something like this

array:2 [▼
  "folder1" => array:1 [▼
    2016 => array:4 [▼
      "1" => array:1 [▶]
      "2" => array:2 [▶]
      "3" => array:4 [▶]
      "4" => array:4 [▶]
    ]
  ]
] 

This is what confuses me. When I run this locally via xampp, the structure is returned in alphabetical order. This is to be expected because when I actually view the folder in Windows it is automatically displayed this way.

However, when I put it on a live server, the structure is pretty random. I end up with something like this

array:2 [▼
  "folder1" => array:1 [▼
    2016 => array:4 [▼
      "2" => array:1 [▶]
      "4" => array:2 [▶]
      "3" => array:4 [▶]
      "1" => array:4 [▶]
    ]
  ]
] 

Is this to do with the operating system the live server is on, which is linux based? How can I make it alphabetical all of the time?

Thanks

Upvotes: 0

Views: 50

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94662

This simple change seems to work

function fillArrayWithFileNodes( DirectoryIterator $dir )
{
    $data = array();
    foreach ( $dir as $node )
    {
        if ( $node->isDir() && !$node->isDot() ) {
            $data[$node->getFilename()] = $this->fillArrayWithFileNodes( new DirectoryIterator( $node->getPathname() ) );
        } else if ( $node->isFile() ) {
            $data[] = $node->getFilename();
        }
    }
    sort($data);
    return $data;
}

You may need to fiddle with the sort() parameters to get exactly waht you want http://php.net/manual/en/function.sort.php

Upvotes: 1

Davide Perozzi
Davide Perozzi

Reputation: 386

For the ksort solution you could do something like (maybe there is something simplier and shorter):

$fileData = array(
    "folder1" => array(
        "4" => array(
            "test"
        ),
        "2" => array(
            "test"
        ),
        "3" => array(
            "test"
        ),
        "1" => array(
            "test"
        ),
        "5" => array(
            "test"
        ),
    )
);

function sortYourArray($arr) {
    ksort($arr);

    foreach ($arr as $k => $v) {
        if (is_array($v)) {
            $arr[$k] = sortYourArray($v);
        }
    }

    return $arr;
}

$sortedArray = sortYourArray($fileData);

print_r($sortedArray);

Upvotes: 1

Related Questions