Need help with sorting array

Need help with sorting array

am 23.12.2007 20:04:24 von Big Moxy

I have a routine to extract the name and mtime files in a directory.
PHP by default sorts the array key. I looked at other sort functions
but don't understand how to sort on a different field.

Can someone please show me how to sort by the filemtime field instead
of the filename?

Thank you!
Tim

border=2>

$dir="./";
if (is_dir($dir)) {
$dh = @opendir($dir);
if($dh) {
while (($file = @readdir($dh)) == true) {
$pos = strpos($file, '.');
if (!$pos === false) {
if ($file != "." && $file != "..") {
$file_array[] = $file;
}
}
}
}
sort($file_array);
reset($file_array);
for($i=0;$i $name=$file_array[$i];
$date = date("Y-m-d H:i", filemtime($name));
if (!is_dir($name)) {
print("
\n");
}
}
}
?>
FileDate
$name$date

Re: Need help with sorting array

am 23.12.2007 20:17:29 von My Pet Programmer

Big Moxy said:
> I have a routine to extract the name and mtime files in a directory.
> PHP by default sorts the array key. I looked at other sort functions
> but don't understand how to sort on a different field.
>
> Can someone please show me how to sort by the filemtime field instead
> of the filename?
>
> Thank you!
> Tim
>
>

> border=2>
>
> > $dir="./";
> if (is_dir($dir)) {
> $dh = @opendir($dir);
> if($dh) {
> while (($file = @readdir($dh)) == true) {
> $pos = strpos($file, '.');
> if (!$pos === false) {
> if ($file != "." && $file != "..") {
> $file_array[] = $file;
> }
> }
> }
> }
> sort($file_array);
> reset($file_array);
> for($i=0;$i > $name=$file_array[$i];
> $date = date("Y-m-d H:i", filemtime($name));
> if (!is_dir($name)) {
> print("
\n");
> }
> }
> }
> ?>
>
FileDate
$name$date


http://www.php.net/manual/en/function.asort.php

RTM

~A!

Re: Need help with sorting array

am 23.12.2007 21:41:17 von Big Moxy

On Dec 23, 11:17=A0am, My Pet Programmer
wrote:
> Big Moxy said:
>
>
>
>
>
> > I have a routine to extract the name and mtime files in a directory.
> > PHP by default sorts the array key. I looked at other sort functions
> > but don't understand how to sort on a different field.
>
> > Can someone please show me how to sort by the filemtime field instead
> > of the filename?
>
> > Thank you!
> > Tim
>
> >

> > border=3D2>
> >
> > > > =A0 $dir=3D"./";
> > =A0 if (is_dir($dir)) {
> > =A0 =A0 $dh =3D @opendir($dir);
> > =A0 =A0 if($dh) {
> > =A0 =A0 =A0 while (($file =3D @readdir($dh)) == true) {
> > =A0 =A0 =A0 =A0 =A0 =A0$pos =3D strpos($file, '.');
> > =A0 =A0 =A0 =A0 =A0 =A0if (!$pos ===3D false) {
> > =A0 =A0 =A0 =A0 =A0 =A0if ($file !=3D "." && $file !=3D "..") {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$file_array[] =3D $file;
> > =A0 =A0 =A0 =A0 =A0 =A0}
> > =A0 =A0 =A0 =A0 =A0 =A0}
> > =A0 =A0 =A0 }
> > =A0 =A0 }
> > =A0 =A0 sort($file_array);
> > =A0 =A0 reset($file_array);
> > =A0 =A0 for($i=3D0;$i > > =A0 =A0 =A0 =A0 =A0 =A0$name=3D$file_array[$i];
> > =A0 =A0 =A0 =A0 $date =3D date("Y-m-d H:i", filemtime($name));
> > =A0 =A0 =A0 =A0 if (!is_dir($name)) {
> > =A0 =A0 =A0 =A0 =A0 =A0print("
\n");=

> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 }
> > =A0 }
> > ?>
> >
FileDate
$name$date

>
> http://www.php.net/manual/en/function.asort.php
>
> RTM
>
> ~A!- Hide quoted text -
>
> - Show quoted text -

The manual may be easy for you to understand but for me the
documentation and subsequent discussion is anything but intuitive. It
also doesn't help that all of the code samples are not commented.

Is there someone who can respond with the understanding that I may not
be an expert PHP programmer?

Re: Need help with sorting array

am 23.12.2007 22:09:32 von My Pet Programmer

Big Moxy said:
> On Dec 23, 11:17 am, My Pet Programmer
> wrote:
>> Big Moxy said:
>>
>>
>>
>>
>>
>>> I have a routine to extract the name and mtime files in a directory.
>>> PHP by default sorts the array key. I looked at other sort functions
>>> but don't understand how to sort on a different field.
>>> Can someone please show me how to sort by the filemtime field instead
>>> of the filename?
>>> Thank you!
>>> Tim
>>>

>>> border=2>
>>>
>>> >>> $dir="./";
>>> if (is_dir($dir)) {
>>> $dh = @opendir($dir);
>>> if($dh) {
>>> while (($file = @readdir($dh)) == true) {
>>> $pos = strpos($file, '.');
>>> if (!$pos === false) {
>>> if ($file != "." && $file != "..") {
>>> $file_array[] = $file;
>>> }
>>> }
>>> }
>>> }
>>> sort($file_array);
>>> reset($file_array);
>>> for($i=0;$i >>> $name=$file_array[$i];
>>> $date = date("Y-m-d H:i", filemtime($name));
>>> if (!is_dir($name)) {
>>> print("
\n");
>>> }
>>> }
>>> }
>>> ?>
>>>
FileDate
$name$date

>> http://www.php.net/manual/en/function.asort.php
>>
>> RTM
>>
>> ~A!- Hide quoted text -
>>
>> - Show quoted text -
>
> The manual may be easy for you to understand but for me the
> documentation and subsequent discussion is anything but intuitive. It
> also doesn't help that all of the code samples are not commented.
>
> Is there someone who can respond with the understanding that I may not
> be an expert PHP programmer?
>
My apologies. You are correct in that I assumed you were much more into
PHP than you are. Comes from spending too much time in the code, I
think. Ok, so here you go, then:

When you run asort on an array, it sorts the array by value, instead of
by key. Since you have an array of filenames you want sorted ascending
(A-Z), you can run this line of code to sort then appropriately:

asort($file_array);

And you don't need to reset the array, your loop looks at them in order,
so you don't need to worry about the internal array pointer.

Sorry for the mix up.

~A!

Re: Need help with sorting array

am 23.12.2007 23:10:59 von Big Moxy

On Dec 23, 1:09=A0pm, My Pet Programmer
wrote:
> Big Moxy said:
>
>
>
> > On Dec 23, 11:17 am, My Pet Programmer
> > wrote:
> >> Big Moxy said:
>
> >>> I have a routine to extract the name and mtime files in a directory.
> >>> PHP by default sorts the array key. I looked at other sort functions
> >>> but don't understand how to sort on a different field.
> >>> Can someone please show me how to sort by the filemtime field instead
> >>> of the filename?
> >>> Thank you!
> >>> Tim
> >>>

0
> >>> border=3D2>
> >>>
> >>> > >>> =A0 $dir=3D"./";
> >>> =A0 if (is_dir($dir)) {
> >>> =A0 =A0 $dh =3D @opendir($dir);
> >>> =A0 =A0 if($dh) {
> >>> =A0 =A0 =A0 while (($file =3D @readdir($dh)) == true) {
> >>> =A0 =A0 =A0 =A0 =A0 =A0$pos =3D strpos($file, '.');
> >>> =A0 =A0 =A0 =A0 =A0 =A0if (!$pos ===3D false) {
> >>> =A0 =A0 =A0 =A0 =A0 =A0if ($file !=3D "." && $file !=3D "..") {
> >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$file_array[] =3D $file;
> >>> =A0 =A0 =A0 =A0 =A0 =A0}
> >>> =A0 =A0 =A0 =A0 =A0 =A0}
> >>> =A0 =A0 =A0 }
> >>> =A0 =A0 }
> >>> =A0 =A0 sort($file_array);
> >>> =A0 =A0 reset($file_array);
> >>> =A0 =A0 for($i=3D0;$i > >>> =A0 =A0 =A0 =A0 =A0 =A0$name=3D$file_array[$i];
> >>> =A0 =A0 =A0 =A0 $date =3D date("Y-m-d H:i", filemtime($name));
> >>> =A0 =A0 =A0 =A0 if (!is_dir($name)) {
> >>> =A0 =A0 =A0 =A0 =A0 =A0print("
\n"=
);
> >>> =A0 =A0 =A0 =A0 }
> >>> =A0 =A0 }
> >>> =A0 }
> >>> ?>
> >>>
FileDate
$name$date

> >>http://www.php.net/manual/en/function.asort.php
>
> >> RTM
>
> >> ~A!- Hide quoted text -
>
> >> - Show quoted text -
>
> > The manual may be easy for you to understand but for me the
> > documentation and subsequent discussion is anything but intuitive. It
> > also doesn't help that all of the code samples are not commented.
>
> > Is there someone who can respond with the understanding that I may not
> > be an expert PHP programmer?
>
> My apologies. You are correct in that I assumed you were much more into
> PHP than you are. Comes from spending too much time in the code, I
> think. Ok, so here you go, then:
>
> When you run asort on an array, it sorts the array by value, instead of
> by key. Since you have an array of filenames you want sorted ascending
> (A-Z), you can run this line of code to sort then appropriately:
>
> asort($file_array);
>
> And you don't need to reset the array, your loop looks at them in order,
> so you don't need to worry about the internal array pointer.
>
> Sorry for the mix up.
>
> ~A!- Hide quoted text -
>
> - Show quoted text -

Thank you. I have enough general coding experience to understand a lot
of code however I was lost without any comments in the manual
examples. My original code contained sort($file_array) which yielded
ascending results by file name. I want to sort by filemtime (date/time
last modified). It seems to me that I have to create a new array and
load name and filemtime a two-dimensional array. Is that correct?

That leads me to a new question. How do I create and load the new
array? I tried this:

// do I explicitly define the new $file_results array? if so,
how?
//
for($i=3D0;$i $name=3D$file_array[$i];
$date =3D date("Y-m-d H:i", filemtime($name));
if (!is_dir($name)) {
$file_results[$i]['date'] =3D $date;
$file_results[$i]['name'] =3D $name;
}
}

which produces:

Date File
Array['date'] Array['name']
Array['date'] Array['name']
Array['date'] Array['name']
Array['date'] Array['name']
Array['date'] Array['name']

Thanks,
Tim

Re: Need help with sorting array

am 23.12.2007 23:18:18 von My Pet Programmer

Big Moxy said:
> On Dec 23, 1:09 pm, My Pet Programmer
> wrote:
>> Big Moxy said:
>>
>>
>>
>>> On Dec 23, 11:17 am, My Pet Programmer
>>> wrote:
>>>> Big Moxy said:
>>>>> I have a routine to extract the name and mtime files in a directory.
>>>>> PHP by default sorts the array key. I looked at other sort functions
>>>>> but don't understand how to sort on a different field.
>>>>> Can someone please show me how to sort by the filemtime field instead
>>>>> of the filename?
>>>>> Thank you!
>>>>> Tim
>>>>>

>>>>> border=2>
>>>>>
>>>>> >>>>> $dir="./";
>>>>> if (is_dir($dir)) {
>>>>> $dh = @opendir($dir);
>>>>> if($dh) {
>>>>> while (($file = @readdir($dh)) == true) {
>>>>> $pos = strpos($file, '.');
>>>>> if (!$pos === false) {
>>>>> if ($file != "." && $file != "..") {
>>>>> $file_array[] = $file;
>>>>> }
>>>>> }
>>>>> }
>>>>> }
>>>>> sort($file_array);
>>>>> reset($file_array);
>>>>> for($i=0;$i >>>>> $name=$file_array[$i];
>>>>> $date = date("Y-m-d H:i", filemtime($name));
>>>>> if (!is_dir($name)) {
>>>>> print("
\n");
>>>>> }
>>>>> }
>>>>> }
>>>>> ?>
>>>>>
FileDate
$name$date

>>>> http://www.php.net/manual/en/function.asort.php
>>>> RTM
>>>> ~A!- Hide quoted text -
>>>> - Show quoted text -
>>> The manual may be easy for you to understand but for me the
>>> documentation and subsequent discussion is anything but intuitive. It
>>> also doesn't help that all of the code samples are not commented.
>>> Is there someone who can respond with the understanding that I may not
>>> be an expert PHP programmer?
>> My apologies. You are correct in that I assumed you were much more into
>> PHP than you are. Comes from spending too much time in the code, I
>> think. Ok, so here you go, then:
>>
>> When you run asort on an array, it sorts the array by value, instead of
>> by key. Since you have an array of filenames you want sorted ascending
>> (A-Z), you can run this line of code to sort then appropriately:
>>
>> asort($file_array);
>>
>> And you don't need to reset the array, your loop looks at them in order,
>> so you don't need to worry about the internal array pointer.
>>
>> Sorry for the mix up.
>>
>> ~A!- Hide quoted text -
>>
>> - Show quoted text -
>
> Thank you. I have enough general coding experience to understand a lot
> of code however I was lost without any comments in the manual
> examples. My original code contained sort($file_array) which yielded
> ascending results by file name. I want to sort by filemtime (date/time
> last modified). It seems to me that I have to create a new array and
> load name and filemtime a two-dimensional array. Is that correct?
>
> That leads me to a new question. How do I create and load the new
> array? I tried this:
>
> // do I explicitly define the new $file_results array? if so,
> how?
> //
> for($i=0;$i > $name=$file_array[$i];
> $date = date("Y-m-d H:i", filemtime($name));
> if (!is_dir($name)) {
> $file_results[$i]['date'] = $date;
> $file_results[$i]['name'] = $name;
> }
> }
>
> which produces:
>
> Date File
> Array['date'] Array['name']
> Array['date'] Array['name']
> Array['date'] Array['name']
> Array['date'] Array['name']
> Array['date'] Array['name']
>
> Thanks,
> Tim

I thought you would be keying on the name and sorting by date, so I was
thinking you could:

for(....) {
$file_array[$name] = $date;
}

asort($file_array);

foreach ($file_array as $name=>$date) {
print $name." -- ".$date;
}

~A!

Re: Need help with sorting array

am 23.12.2007 23:59:05 von Big Moxy

On Dec 23, 2:18=A0pm, My Pet Programmer
wrote:
> Big Moxy said:
>
>
>
>
>
> > On Dec 23, 1:09 pm, My Pet Programmer
> > wrote:
> >> Big Moxy said:
>
> >>> On Dec 23, 11:17 am, My Pet Programmer
> >>> wrote:
> >>>> Big Moxy said:
> >>>>> I have a routine to extract the name and mtime files in a directory.=

> >>>>> PHP by default sorts the array key. I looked at other sort functions=

> >>>>> but don't understand how to sort on a different field.
> >>>>> Can someone please show me how to sort by the filemtime field instea=
d
> >>>>> of the filename?
> >>>>> Thank you!
> >>>>> Tim
> >>>>>

=3D0
> >>>>> border=3D2>
> >>>>>
> >>>>> > >>>>> =A0 $dir=3D"./";
> >>>>> =A0 if (is_dir($dir)) {
> >>>>> =A0 =A0 $dh =3D @opendir($dir);
> >>>>> =A0 =A0 if($dh) {
> >>>>> =A0 =A0 =A0 while (($file =3D @readdir($dh)) == true) {
> >>>>> =A0 =A0 =A0 =A0 =A0 =A0$pos =3D strpos($file, '.');
> >>>>> =A0 =A0 =A0 =A0 =A0 =A0if (!$pos ===3D false) {
> >>>>> =A0 =A0 =A0 =A0 =A0 =A0if ($file !=3D "." && $file !=3D "..") {
> >>>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$file_array[] =3D $file;
> >>>>> =A0 =A0 =A0 =A0 =A0 =A0}
> >>>>> =A0 =A0 =A0 =A0 =A0 =A0}
> >>>>> =A0 =A0 =A0 }
> >>>>> =A0 =A0 }
> >>>>> =A0 =A0 sort($file_array);
> >>>>> =A0 =A0 reset($file_array);
> >>>>> =A0 =A0 for($i=3D0;$i > >>>>> =A0 =A0 =A0 =A0 =A0 =A0$name=3D$file_array[$i];
> >>>>> =A0 =A0 =A0 =A0 $date =3D date("Y-m-d H:i", filemtime($name));
> >>>>> =A0 =A0 =A0 =A0 if (!is_dir($name)) {
> >>>>> =A0 =A0 =A0 =A0 =A0 =A0print("
\=
n");
> >>>>> =A0 =A0 =A0 =A0 }
> >>>>> =A0 =A0 }
> >>>>> =A0 }
> >>>>> ?>
> >>>>>
FileDate
$name$date

> >>>>http://www.php.net/manual/en/function.asort.php
> >>>> RTM
> >>>> ~A!- Hide quoted text -
> >>>> - Show quoted text -
> >>> The manual may be easy for you to understand but for me the
> >>> documentation and subsequent discussion is anything but intuitive. It
> >>> also doesn't help that all of the code samples are not commented.
> >>> Is there someone who can respond with the understanding that I may not=

> >>> be an expert PHP programmer?
> >> My apologies. You are correct in that I assumed you were much more into=

> >> PHP than you are. Comes from spending too much time in the code, I
> >> think. Ok, so here you go, then:
>
> >> When you run asort on an array, it sorts the array by value, instead of=

> >> by key. Since you have an array of filenames you want sorted ascending
> >> (A-Z), you can run this line of code to sort then appropriately:
>
> >> asort($file_array);
>
> >> And you don't need to reset the array, your loop looks at them in order=
,
> >> so you don't need to worry about the internal array pointer.
>
> >> Sorry for the mix up.
>
> >> ~A!- Hide quoted text -
>
> >> - Show quoted text -
>
> > Thank you. I have enough general coding experience to understand a lot
> > of code however I was lost without any comments in the manual
> > examples. My original code contained sort($file_array) which yielded
> > ascending results by file name. I want to sort by filemtime (date/time
> > last modified). It seems to me that I have to create a new array and
> > load name and filemtime a two-dimensional array. Is that correct?
>
> > That leads me to a new question. How do I create and load the new
> > array? I tried this:
>
> > =A0 =A0 =A0 =A0 // do I explicitly define the new $file_results array? i=
f so,
> > how?
> > =A0 =A0 =A0 =A0 //
> > =A0 =A0for($i=3D0;$i > > =A0 =A0 =A0 =A0 =A0 =A0$name=3D$file_array[$i];
> > =A0 =A0 =A0 =A0 $date =3D date("Y-m-d H:i", filemtime($name));
> > =A0 =A0 =A0 =A0 if (!is_dir($name)) {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$file_results[$i]['date'] =3D $da=
te;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$file_results[$i]['name'] =3D $na=
me;
> > =A0 =A0 =A0 =A0 =A0 =A0}
> > =A0 =A0 }
>
> > which produces:
>
> > =A0 =A0 =A0 =A0Date =A0 =A0 =A0 =A0File
> > Array['date'] Array['name']
> > Array['date'] Array['name']
> > Array['date'] Array['name']
> > Array['date'] Array['name']
> > Array['date'] Array['name']
>
> > Thanks,
> > Tim
>
> I thought you would be keying on the name and sorting by date, so I was
> thinking you could:
>
> for(....) {
> =A0 =A0$file_array[$name] =3D $date;
>
> }
>
> asort($file_array);
>
> foreach ($file_array as $name=3D>$date) {
> =A0 print $name." -- ".$date;
>
> }
>
> ~A!- Hide quoted text -
>
> - Show quoted text -

We're almost there!! url - http://projects.missioninternet.com/proweb/client=
s/dirlist.php

I used arsort and you can see the file dates are sorted in descending
order. My question is how do I get rid of the other array elements?

for($i=3D0;$i $name=3D$file_array[$i];
$date =3D date("Y-m-d H:i", filemtime($name));
if (!is_dir($name)) {
$file_array[$name] =3D $date;
}
}
arsort($file_array);
foreach ($file_array as $name=3D>$date) {
print("".$date."".$name."\n");
}

Re: Need help with sorting array

am 24.12.2007 00:23:57 von Big Moxy

On Dec 23, 2:59=A0pm, Big Moxy wrote:
> On Dec 23, 2:18=A0pm, My Pet Programmer
> wrote:
>
>
>
>
>
> > Big Moxy said:
>
> > > On Dec 23, 1:09 pm, My Pet Programmer
> > > wrote:
> > >> Big Moxy said:
>
> > >>> On Dec 23, 11:17 am, My Pet Programmer =

> > >>> wrote:
> > >>>> Big Moxy said:
> > >>>>> I have a routine to extract the name and mtime files in a director=
y.
> > >>>>> PHP by default sorts the array key. I looked at other sort functio=
ns
> > >>>>> but don't understand how to sort on a different field.
> > >>>>> Can someone please show me how to sort by the filemtime field inst=
ead
> > >>>>> of the filename?
> > >>>>> Thank you!
> > >>>>> Tim
> > >>>>>

G=3D0
> > >>>>> border=3D2>
> > >>>>>
> > >>>>> > > >>>>> =A0 $dir=3D"./";
> > >>>>> =A0 if (is_dir($dir)) {
> > >>>>> =A0 =A0 $dh =3D @opendir($dir);
> > >>>>> =A0 =A0 if($dh) {
> > >>>>> =A0 =A0 =A0 while (($file =3D @readdir($dh)) == true) {
> > >>>>> =A0 =A0 =A0 =A0 =A0 =A0$pos =3D strpos($file, '.');
> > >>>>> =A0 =A0 =A0 =A0 =A0 =A0if (!$pos ===3D false) {
> > >>>>> =A0 =A0 =A0 =A0 =A0 =A0if ($file !=3D "." && $file !=3D "..") {
> > >>>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$file_array[] =3D $file;
> > >>>>> =A0 =A0 =A0 =A0 =A0 =A0}
> > >>>>> =A0 =A0 =A0 =A0 =A0 =A0}
> > >>>>> =A0 =A0 =A0 }
> > >>>>> =A0 =A0 }
> > >>>>> =A0 =A0 sort($file_array);
> > >>>>> =A0 =A0 reset($file_array);
> > >>>>> =A0 =A0 for($i=3D0;$i > > >>>>> =A0 =A0 =A0 =A0 =A0 =A0$name=3D$file_array[$i];
> > >>>>> =A0 =A0 =A0 =A0 $date =3D date("Y-m-d H:i", filemtime($name));
> > >>>>> =A0 =A0 =A0 =A0 if (!is_dir($name)) {
> > >>>>> =A0 =A0 =A0 =A0 =A0 =A0print("
>\n");
> > >>>>> =A0 =A0 =A0 =A0 }
> > >>>>> =A0 =A0 }
> > >>>>> =A0 }
> > >>>>> ?>
> > >>>>>
FileDate
$name$date

> > >>>>http://www.php.net/manual/en/function.asort.php
> > >>>> RTM
> > >>>> ~A!- Hide quoted text -
> > >>>> - Show quoted text -
> > >>> The manual may be easy for you to understand but for me the
> > >>> documentation and subsequent discussion is anything but intuitive. I=
t
> > >>> also doesn't help that all of the code samples are not commented.
> > >>> Is there someone who can respond with the understanding that I may n=
ot
> > >>> be an expert PHP programmer?
> > >> My apologies. You are correct in that I assumed you were much more in=
to
> > >> PHP than you are. Comes from spending too much time in the code, I
> > >> think. Ok, so here you go, then:
>
> > >> When you run asort on an array, it sorts the array by value, instead =
of
> > >> by key. Since you have an array of filenames you want sorted ascendin=
g
> > >> (A-Z), you can run this line of code to sort then appropriately:
>
> > >> asort($file_array);
>
> > >> And you don't need to reset the array, your loop looks at them in ord=
er,
> > >> so you don't need to worry about the internal array pointer.
>
> > >> Sorry for the mix up.
>
> > >> ~A!- Hide quoted text -
>
> > >> - Show quoted text -
>
> > > Thank you. I have enough general coding experience to understand a lot=

> > > of code however I was lost without any comments in the manual
> > > examples. My original code contained sort($file_array) which yielded
> > > ascending results by file name. I want to sort by filemtime (date/time=

> > > last modified). It seems to me that I have to create a new array and
> > > load name and filemtime a two-dimensional array. Is that correct?
>
> > > That leads me to a new question. How do I create and load the new
> > > array? I tried this:
>
> > > =A0 =A0 =A0 =A0 // do I explicitly define the new $file_results array?=
if so,
> > > how?
> > > =A0 =A0 =A0 =A0 //
> > > =A0 =A0for($i=3D0;$i > > > =A0 =A0 =A0 =A0 =A0 =A0$name=3D$file_array[$i];
> > > =A0 =A0 =A0 =A0 $date =3D date("Y-m-d H:i", filemtime($name));
> > > =A0 =A0 =A0 =A0 if (!is_dir($name)) {
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$file_results[$i]['date'] =3D $=
date;
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$file_results[$i]['name'] =3D $=
name;
> > > =A0 =A0 =A0 =A0 =A0 =A0}
> > > =A0 =A0 }
>
> > > which produces:
>
> > > =A0 =A0 =A0 =A0Date =A0 =A0 =A0 =A0File
> > > Array['date'] Array['name']
> > > Array['date'] Array['name']
> > > Array['date'] Array['name']
> > > Array['date'] Array['name']
> > > Array['date'] Array['name']
>
> > > Thanks,
> > > Tim
>
> > I thought you would be keying on the name and sorting by date, so I was
> > thinking you could:
>
> > for(....) {
> > =A0 =A0$file_array[$name] =3D $date;
>
> > }
>
> > asort($file_array);
>
> > foreach ($file_array as $name=3D>$date) {
> > =A0 print $name." -- ".$date;
>
> > }
>
> > ~A!- Hide quoted text -
>
> > - Show quoted text -
>
> We're almost there!! url -http://projects.missioninternet.com/proweb/clien=
ts/dirlist.php
>
> I used arsort and you can see the file dates are sorted in descending
> order. My question is how do I get rid of the other array elements?
>
> =A0 =A0 =A0 =A0 for($i=3D0;$i > =A0 =A0 =A0 =A0 $name=3D$file_array[$i];
> =A0 =A0 =A0 =A0 $date =3D date("Y-m-d H:i", filemtime($name));
> =A0 =A0 =A0 =A0 if (!is_dir($name)) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $file_array[$name] =3D $da=
te;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 }
> =A0 =A0 arsort($file_array);
> =A0 =A0 =A0 =A0 foreach ($file_array as $name=3D>$date) {
> =A0 =A0 =A0 =A0 print("".$date."".$name."\n");
> =A0 =A0 }- Hide quoted text -
>
> - Show quoted text -

~A!

I added a dirty edit based on my test data:

foreach ($file_array as $name=3D>$date) {
if ((is_numeric(substr($date,0,4))) && (substr($date,0,4) !=3D
'1969')) {
print("".$date."".$name."\n");
}
}

If there is a better way please let me know!

Thanks for your help!
Tim

Re: Need help with sorting array

am 24.12.2007 00:26:34 von My Pet Programmer

Big Moxy said:
> On Dec 23, 2:18 pm, My Pet Programmer
> wrote:
>> Big Moxy said:
>>
>>
>>
>>
>>
>>> On Dec 23, 1:09 pm, My Pet Programmer
>>> wrote:
>>>> Big Moxy said:
>>>>> On Dec 23, 11:17 am, My Pet Programmer
>>>>> wrote:
>>>>>> Big Moxy said:
>>>>>>> I have a routine to extract the name and mtime files in a directory.
>>>>>>> PHP by default sorts the array key. I looked at other sort functions
>>>>>>> but don't understand how to sort on a different field.
>>>>>>> Can someone please show me how to sort by the filemtime field instead
>>>>>>> of the filename?
>>>>>>> Thank you!
>>>>>>> Tim
>>>>>>>

>>>>>>> border=2>
>>>>>>>
>>>>>>> >>>>>>> $dir="./";
>>>>>>> if (is_dir($dir)) {
>>>>>>> $dh = @opendir($dir);
>>>>>>> if($dh) {
>>>>>>> while (($file = @readdir($dh)) == true) {
>>>>>>> $pos = strpos($file, '.');
>>>>>>> if (!$pos === false) {
>>>>>>> if ($file != "." && $file != "..") {
>>>>>>> $file_array[] = $file;
>>>>>>> }
>>>>>>> }
>>>>>>> }
>>>>>>> }
>>>>>>> sort($file_array);
>>>>>>> reset($file_array);
>>>>>>> for($i=0;$i >>>>>>> $name=$file_array[$i];
>>>>>>> $date = date("Y-m-d H:i", filemtime($name));
>>>>>>> if (!is_dir($name)) {
>>>>>>> print("
\n");
>>>>>>> }
>>>>>>> }
>>>>>>> }
>>>>>>> ?>
>>>>>>>
FileDate
$name$date

>>>>>> http://www.php.net/manual/en/function.asort.php
>>>>>> RTM
>>>>>> ~A!- Hide quoted text -
>>>>>> - Show quoted text -
>>>>> The manual may be easy for you to understand but for me the
>>>>> documentation and subsequent discussion is anything but intuitive. It
>>>>> also doesn't help that all of the code samples are not commented.
>>>>> Is there someone who can respond with the understanding that I may not
>>>>> be an expert PHP programmer?
>>>> My apologies. You are correct in that I assumed you were much more into
>>>> PHP than you are. Comes from spending too much time in the code, I
>>>> think. Ok, so here you go, then:
>>>> When you run asort on an array, it sorts the array by value, instead of
>>>> by key. Since you have an array of filenames you want sorted ascending
>>>> (A-Z), you can run this line of code to sort then appropriately:
>>>> asort($file_array);
>>>> And you don't need to reset the array, your loop looks at them in order,
>>>> so you don't need to worry about the internal array pointer.
>>>> Sorry for the mix up.
>>>> ~A!- Hide quoted text -
>>>> - Show quoted text -
>>> Thank you. I have enough general coding experience to understand a lot
>>> of code however I was lost without any comments in the manual
>>> examples. My original code contained sort($file_array) which yielded
>>> ascending results by file name. I want to sort by filemtime (date/time
>>> last modified). It seems to me that I have to create a new array and
>>> load name and filemtime a two-dimensional array. Is that correct?
>>> That leads me to a new question. How do I create and load the new
>>> array? I tried this:
>>> // do I explicitly define the new $file_results array? if so,
>>> how?
>>> //
>>> for($i=0;$i >>> $name=$file_array[$i];
>>> $date = date("Y-m-d H:i", filemtime($name));
>>> if (!is_dir($name)) {
>>> $file_results[$i]['date'] = $date;
>>> $file_results[$i]['name'] = $name;
>>> }
>>> }
>>> which produces:
>>> Date File
>>> Array['date'] Array['name']
>>> Array['date'] Array['name']
>>> Array['date'] Array['name']
>>> Array['date'] Array['name']
>>> Array['date'] Array['name']
>>> Thanks,
>>> Tim
>> I thought you would be keying on the name and sorting by date, so I was
>> thinking you could:
>>
>> for(....) {
>> $file_array[$name] = $date;
>>
>> }
>>
>> asort($file_array);
>>
>> foreach ($file_array as $name=>$date) {
>> print $name." -- ".$date;
>>
>> }
>>
>> ~A!- Hide quoted text -
>>
>> - Show quoted text -
>
> We're almost there!! url - http://projects.missioninternet.com/proweb/clients/dirlist.p hp
>
> I used arsort and you can see the file dates are sorted in descending
> order. My question is how do I get rid of the other array elements?
>
> for($i=0;$i > $name=$file_array[$i];
> $date = date("Y-m-d H:i", filemtime($name));
> if (!is_dir($name)) {
> $file_array[$name] = $date;
> }
> }
> arsort($file_array);
> foreach ($file_array as $name=>$date) {
> print("".$date."".$name."\n");
> }

Oh, sorry, I should have used a different variable name for the output
array.

$outArray = array();
for($i=0;$i $name=$file_array[$i];
$date = date("Y-m-d H:i", filemtime($name));
if (!is_dir($name)) {
$outArray[$name] = $date;
}
}

arsort($out_array);

foreach ($outArray as $name=>$date) {
print("".$date."".$name."\n");
}

Then You should be all set.

~A!