/    Sign up×
Community /Pin to ProfileBookmark

Multi-dimensional array sorting guidance needed…

Good day to you,
I have an multi-dimensional array and I need to sort it by date and then height.

My question is how would I build my code to do such a function ?

Here is how I have built my array :

[code=php]

<?php

$imgdir= $_GET[‘folder’];
$imgdir.=”/”;

function dircont($imgdir)
{
$dir_array = array();
if (false !== ($dir = opendir($imgdir)))
{
while (false !== ($file = readdir($dir)))
{
if ($file != ‘.’ && $file != ‘..’)
{
$fInfo = array();
$fInfo[] = $file;
if (($imageInfo = @getImageSize($imgdir.””.$file)) !== false)
{
$fInfo[‘width’] = $imageInfo[0];
$fInfo[‘height’] = $imageInfo[1];
}

if (($stat = stat($imgdir.””.$file)) !== false)
{
$fInfo[‘mtime’] = $stat[‘mtime’];
// Lol, forgot to put the size in 😛
$fInfo[‘size’] = $stat[‘size’];
}

$dir_array[] = $fInfo;
}
}
return $dir_array;
}
else
{
return false;
}
}

// To print..
echo “<pre>”;
print_r(dircont($imgdir));
echo “</pre>”;

?>

[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 26.2008 — This typically requires the use of the [url=www.php.net/usort]usort() function[/url].
[code=php]
function mysort($a, $b)
{
if($a['mtime'] == $b['mtime'])
{
return $a['height'] - $b['height'];
}
else
{
return $a['mtime'] - $b['mtime'];
}
}
usort($dir_array, 'mysort');
[/code]

Depending on whether you want ascending or descending sorts, you may need to invert the $a/$b order in the comparison statements.
×

Success!

Help @Peuplarchie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...