![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New Coder ![]() Join Date: Oct 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
sort array in item order when numbers go over 10
how do i sort the following file examples so they are in number order?
abc - 23-11-2009 - Agenda Item 1 - Name of item 1.pdf abc - 23-11-2009 - Agenda Item 2 - Name of item 2.pdf abc - 23-11-2009 - Agenda Item 3 - Name of item 3.pdf abc - 23-11-2009 - Agenda Item 4 - Name of item 4.pdf abc - 23-11-2009 - Agenda Item 5 - Name of item 5.pdf abc - 23-11-2009 - Agenda Item 6 - Name of item 6.pdf abc - 23-11-2009 - Agenda Item 7 - Name of item 7.pdf abc - 23-11-2009 - Agenda Item 8 - Name of item 8.pdf abc - 23-11-2009 - Agenda Item 9 - Name of item 9.pdf abc - 23-11-2009 - Agenda Item 10 - Name of item 10.pdf abc - 23-11-2009 - Agenda Item 11 - Name of item 11.pdf abc - 23-11-2009 - Agenda Item 12 - Name of item 12.pdf as it is showing as abc - 23-11-2009 - Agenda Item 1 - Name of item 1.pdf abc - 23-11-2009 - Agenda Item 10 - Name of item 10.pdf abc - 23-11-2009 - Agenda Item 11 - Name of item 11.pdf abc - 23-11-2009 - Agenda Item 12 - Name of item 12.pdf abc - 23-11-2009 - Agenda Item 2 - Name of item 2.pdf abc - 23-11-2009 - Agenda Item 3 - Name of item 3.pdf abc - 23-11-2009 - Agenda Item 4 - Name of item 4.pdf abc - 23-11-2009 - Agenda Item 5 - Name of item 5.pdf abc - 23-11-2009 - Agenda Item 6 - Name of item 6.pdf abc - 23-11-2009 - Agenda Item 7 - Name of item 7.pdf abc - 23-11-2009 - Agenda Item 8 - Name of item 8.pdf abc - 23-11-2009 - Agenda Item 9 - Name of item 9.pdf i used the natsort but this did not sort the file names into order. this my code with natsort.. Code:
<?
$files = glob('agendas/*Agenda Item*.pdf');
$sortfile = array();
foreach($files as $file){
//Extract date
if (preg_match('/(\d+)-(\d+)-(\d+)[^.]*?\.pdf/', $file, $reg)) {
//add date to array (file as key)
$sortfile[$file] = mktime(0,0,0,$reg[2],$reg[1],$reg[3]);
}
}
natsort($sortfile); //Sort an array (the dates) and maintain index association (file names)
$sortfile = array_keys($sortfile);
foreach ($files as $key=>$value) {
$datestring = strtotime(basename(substr($sortfile[$key], 14, 10),".pdf"));
// $anchortext = date('j M Y',$datestring);
$itemnumber = basename(substr($sortfile[$key], 39, 2),".pdf");
$itemname = basename(substr($sortfile[$key], 43),".pdf");
$url = date('dmY',$datestring);
?><li><a href="?a=<?=$url;?>"><? if (strlen(date('j',$datestring)) == 1) { ?> <? } ?><strong><?=$itemnumber."=".$itemname;?></strong></a><? if (strlen(date('j',$datestring)) == 1) { ?> <? }
} ?></li>
|
|
|
|
|
|
PM User | #2 |
|
Master Coder ![]() Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 5,405
Thanks: 32
Thanked 373 Times in 364 Posts
![]() ![]() ![]() ![]() ![]() |
natsort() works fine for this, but your array's values aren't the filenames, your array INDEXes are the filenames. I don't see a ksort() option to do a natural sort, so you'll have to use array_flip(), natsort(), then flip back.
__________________
|
|
|
|
|
|
PM User | #3 |
|
New Coder ![]() Join Date: Oct 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
thanks for you reply but still not getting the files to show in number order.
i was not sure where i do the sort or flip and messed around with the code but could not get it in order i even put a leading '0' in the file names and still it did not show in order. can you see where i may be going wrong with my code? Code:
<?
$files = glob('agendas/*Agenda Item*.pdf');
$sortfile = array();
foreach($files as $file){
//Extract date
if (preg_match('/(\d+)-(\d+)-(\d+)[^.]*?\.pdf/', $file, $reg)) {
//add date to array (file as key)
$sortfile[$file] = mktime(0,0,0,$reg[2],$reg[1],$reg[3]);
}
}
// natsort($sortfile); //Sort an array (the dates) and maintain index association (file names)
//array_flip($sortfile);
natsort($sortfile); //Sort an array (the dates) and maintain index association (file names)
array_flip($sortfile);
$sortfile = array_keys($sortfile);
foreach ($files as $key=>$value) {
$datestring = strtotime(basename(substr($sortfile[$key], 14, 10),".pdf"));
// $anchortext = date('j M Y',$datestring);
$itemnumber = basename(substr($sortfile[$key], 39, 2),".pdf");
$itemname = basename(substr($sortfile[$key], 43),".pdf");
$url = date('dmY',$datestring);
?><li><a href="?a=<?=$url.'&in='.$itemnumber;?>"><?
if (strlen(date('j',$datestring)) == 1) { ?> <?
}
?><strong><?=$itemnumber."=".$itemname;?></strong></a><?
if (strlen(date('j',$datestring)) == 1) { ?> <?
}
} ?></li>
|
|
|
|
|
|
PM User | #4 | |
|
New Coder ![]() Join Date: Oct 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Quote:
i tried to use the array_flip and that but not having much luck as yet playing around with the lines of code moving them around to see what it shows by outputting the data each array at a time. |
|
|
|
|
|
|
PM User | #5 |
|
Master Coder ![]() Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 5,405
Thanks: 32
Thanked 373 Times in 364 Posts
![]() ![]() ![]() ![]() ![]() |
You need to understand your code if you are to ever have a hope of getting it straight.
The array_flip switches each array element's key with its value. If that makes no sense to you then take a step back and study up on arrays, associative arrays, etc.
__________________
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|