开发者

DirectoryIterator and array_pop and foreachloop of array

开发者 https://www.devze.com 2023-03-30 22:09 出处:网络
I have this snippet $clusters = new DirectoryIterator(\"/agents/\"); $cluster_array = array(); # Push each cluster into an array

I have this snippet

$clusters = new DirectoryIterator("/agents/");
$cluster_array = array();

# Push each cluster into an array
foreach ($clusters as $fileinfo) {
    if ($fileinfo != "." && $fileinfo != ".." && $fileinfo != "conf") {
        array_push($cluster_array, $fileinfo->getFilename());
    }
}

this puts each folder it finds into my array, but my problem is that it keeps pushing the results at the begining of the array.

array([0]=>cluster_2, [1]=>cluster_1)

I want it to push the folders in order at the end of the array. I tried array_pop but i wasn't sure how to use it in this context.

After I have my array built, I wanted to scan thru each array and see how much free space was left with

foreach ($cluster_array as $cluster) {
    echo $cluster . " " . Server::server_free_space("/agents/" . $cluster, 2)."<br />";
}

that class method just uses disk_free_space() then converts the results into MB for me.

in-place of the echo statement I was going to do something like

if ((Server::server_total_space("/agents/" . $cluster, 2) / 3) > xMB) {

}

I wanted to create a folder into the first item in the array that has xMB availa开发者_开发问答ble, if xMB isn't available then move onto the next array item and so on until it finds an array item that has the space required to make a new folder.

does this make sense?

EDIT:

I ended up doing this an build the array kinda manually

$clusters = new DirectoryIterator("/agents/");
$cluster_array = array();
$cluster_count = 0;

# Push each cluster into an array
foreach ($clusters as $fileinfo) {
    if ($fileinfo != "." && $fileinfo != ".." && $fileinfo != "conf") {
        array_push($cluster_array, "cluster_" . $cluster_count += 1);
    }
}


You are using array_push() correctly, however DirectoryIterator and related iterators iterate through the items of the underlying filesystem, the way the operating system provides them. This is not necessarily sorted the way one might expect (naturally sorted, for instance). For a little more on this, also see this faux bug report.

0

精彩评论

暂无评论...
验证码 换一张
取 消