0

I have an array inside an array that has the filename, modification time and size, but I need to be able to order the array either ascending or descending by each one of these properties.

I have the following, which gets the information

//SCAN THE DIRECTORY
$directories = scandir($dir);
$directinfo = array();
foreach($directories as $directory){
    if ($directory === '.' or $directory === '..') continue;
    if(!stat($dir.'/'.$directory)){

    } else {
        $filestat = stat($dir.'/'.$directory);
        $directinfo[] = array(
            'name' => $directory,
            'modtime' => $filestat['mtime'],
            'size' => $filestat['size']
        );
    }
}

The array is structured as so:

Array ( 
    [0] => Array ( 
        [name] => 0 Organisation Details 
        [modtime] => 1398164749 
        [size] => 4096
    ) 
    [1] => Array ( 
        [name] => 1 Permission Form 
        [modtime] => 1398164749 
        [size] => 4096 
    ) 
    [2] => Array ( 
        [name] => 6 Invoices 
        [modtime] => 1400802471 
        [size] => 4096 
    ) 
)

and then use this to output:

foreach($directinfo as $dirInfo){
    foreach($dirInfo as $key=>$drInfo){
        echo "Output: ".$key."=>".$drInfo."<br />";
    }
}

But I need to arrange the array before this, and somehow make it so I don't need two arrays or I'm suspecting the ordered output wouldn't work.

I've looked at array_multisort but can't figure out how this would work in this instance.

Any help with this is really appreciated.

2
  • 1
    There's a great table in the manual describing which sorting function will be suitable for which scenario. Otherwise, can you provide an example of your array structure? Commented Jul 13, 2014 at 21:58
  • 1
    You can save one call to stat with if (!($filestat = stat ...)) { } else { $directinfo[] = .... Commented Jul 13, 2014 at 22:12

2 Answers 2

1

You can use usort:

usort($arr, function($a,$b){
    return strcmp($a['name'], $b['name']);
});

var_dump($arr);

http://codepad.viper-7.com/lZoUsO

Sign up to request clarification or add additional context in comments.

3 Comments

Not sure how this helps, as that assumes I have one array, whereas I have an array inside an array. So how would I use this, given the data I've provided?
@SeanMcCabe you haven't actually provided any data, it would definitely help if you did
@SeánMcCabe Please check the fiddle - it is working exactly how you require. $a and $b are elements of the array, themselves being individual arrays. The array $arr is sorted on the name elements of the internal arrays
0

Using example #3 on the array_multisort manual page:

$directories = array( 
array('name'=>'filec','modtime'=>'12303403434','size'=>'12401'),
array('name'=>'fileb','modtime'=>'12303403432','size'=>'12400'), 
array('name'=>'filez', 'modtime'=>'12304445405','size'=>'65200')
);

// Obtain a list of columns
$name = $modtime = $size = array();
foreach ($directories as $key => $row) {
    $name[$key]  = $row['name'];
    $modtime[$key] = $row['modtime'];
    $size[$key] = $row['size'];
}

// Sort the data with name ascending, modtime ascending, size ascending
array_multisort($name, SORT_ASC, $modtime, SORT_ASC, $size, SORT_ASC, $directories);

print_r($directories);

Online demo here

1 Comment

Thank you @cOle2 its worked perfectly. I had come across this before but couldnt get it working, but it is now.

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.