Bluehost.com Web Hosting $6.95

Ordering array won't work?

This is a discussion on Ordering array won't work? within the PHP Language forums, part of the PHP Programming Forums category; Hi there, I want to scan a dir, put the *.gif into an array, and order that ( 01.gif, 02....


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-25-2006
frizzle
 
Posts: n/a
Default Ordering array won't work?

Hi there,

I want to scan a dir, put the *.gif into an array, and order that (
01.gif, 02.gif etc),
and put that array into a $_session-array. (Session & Headers are
initiated before this code)
I have the code below, everything works, except for the ordering.

Any clues ?

Thanks, Frizzle.

$dir = opendir("./img/");

while( $file = readdir($dir)){
if (preg_match("/\.gif$/", $file)) {

$filename = preg_replace( "/\.gif$/", '', $file );
$_SESSION['all_pics'][$filename] = "Foobar";

};
};

closedir( $dir );

asort( $_SESSION['all_pics'] );

Reply With Quote
  #2 (permalink)  
Old 10-25-2006
Andy Hassall
 
Posts: n/a
Default Re: Ordering array won't work?

On 25 Oct 2006 11:06:14 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:

>I want to scan a dir, put the *.gif into an array, and order that (
>01.gif, 02.gif etc),
>and put that array into a $_session-array. (Session & Headers are
>initiated before this code)
>I have the code below, everything works, except for the ordering.
>
> $filename = preg_replace( "/\.gif$/", '', $file );
> $_SESSION['all_pics'][$filename] = "Foobar";
>
>asort( $_SESSION['all_pics'] );


All the values in your array are equal to "Foobar", and so asort doesn't need
to do anything to the array to sort it.

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Reply With Quote
  #3 (permalink)  
Old 10-25-2006
Rik
 
Posts: n/a
Default Re: Ordering array won't work?

frizzle wrote:
> Hi there,
>
> I want to scan a dir, put the *.gif into an array, and order that (
> 01.gif, 02.gif etc),
> and put that array into a $_session-array. (Session & Headers are
> initiated before this code)
> I have the code below, everything works, except for the ordering.
>
> Any clues ?


> $_SESSION['all_pics'][$filename] = "Foobar";


> asort( $_SESSION['all_pics'] );


Well, if the value of every array item is the same, offcourse there will be
no sorting done.
If you want to sort on key:
ksort($_SESSION['all_pics']);

Allthough I can't image why:
$_SESSION['all_pics'][] = $filename;
...
asort($_SESSION['all_pics']);
does not do...
--
Rik Wasmus


Reply With Quote
  #4 (permalink)  
Old 10-25-2006
frizzle
 
Posts: n/a
Default Re: Ordering array won't work?


Andy Hassall wrote:
> On 25 Oct 2006 11:06:14 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:
>
> >I want to scan a dir, put the *.gif into an array, and order that (
> >01.gif, 02.gif etc),
> >and put that array into a $_session-array. (Session & Headers are
> >initiated before this code)
> >I have the code below, everything works, except for the ordering.
> >
> > $filename = preg_replace( "/\.gif$/", '', $file );
> > $_SESSION['all_pics'][$filename] = "Foobar";
> >
> >asort( $_SESSION['all_pics'] );

>
> All the values in your array are equal to "Foobar", and so asort doesn't need
> to do anything to the array to sort it.
>
> --
> Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
> http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


I'm sorry, i guess i wasn't clear.
I want it ordered by it's keys ...

$_SESSION['all_pics'][$filename]
---------------------------------------^

Thanks for the fast reply.

Frizzle.

Reply With Quote
  #5 (permalink)  
Old 10-25-2006
Andy Hassall
 
Posts: n/a
Default Re: Ordering array won't work?

On 25 Oct 2006 11:29:15 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:

>Andy Hassall wrote:
>> On 25 Oct 2006 11:06:14 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:
>>
>>>I want to scan a dir, put the *.gif into an array, and order that (
>>>01.gif, 02.gif etc),
>>>and put that array into a $_session-array. (Session & Headers are
>>>initiated before this code)
>>>I have the code below, everything works, except for the ordering.
>>>
>>> $filename = preg_replace( "/\.gif$/", '', $file );
>>> $_SESSION['all_pics'][$filename] = "Foobar";
>>>
>>>asort( $_SESSION['all_pics'] );

>>
>> All the values in your array are equal to "Foobar", and so asort doesn't need
>> to do anything to the array to sort it.

>
>I'm sorry, i guess i wasn't clear.
>I want it ordered by it's keys ...
>
>$_SESSION['all_pics'][$filename]


Start at http://uk2.php.net/manual/en/ref.array.php

Look at all the various functions with "sort" in their name. You'll find the
one you want soon enough.

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Reply With Quote
  #6 (permalink)  
Old 10-25-2006
frizzle
 
Posts: n/a
Default Re: Ordering array won't work?


Andy Hassall wrote:
> On 25 Oct 2006 11:29:15 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:
>
> >Andy Hassall wrote:
> >> On 25 Oct 2006 11:06:14 -0700, "frizzle" <phpfrizzle@gmail.com> wrote:
> >>
> >>>I want to scan a dir, put the *.gif into an array, and order that (
> >>>01.gif, 02.gif etc),
> >>>and put that array into a $_session-array. (Session & Headers are
> >>>initiated before this code)
> >>>I have the code below, everything works, except for the ordering.
> >>>
> >>> $filename = preg_replace( "/\.gif$/", '', $file );
> >>> $_SESSION['all_pics'][$filename] = "Foobar";
> >>>
> >>>asort( $_SESSION['all_pics'] );
> >>
> >> All the values in your array are equal to "Foobar", and so asort doesn't need
> >> to do anything to the array to sort it.

> >
> >I'm sorry, i guess i wasn't clear.
> >I want it ordered by it's keys ...
> >
> >$_SESSION['all_pics'][$filename]

>
> Start at http://uk2.php.net/manual/en/ref.array.php
>
> Look at all the various functions with "sort" in their name. You'll find the
> one you want soon enough.
>
> --
> Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
> http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


Great! Ksort. Can't believe i missed that.
Thanks again.

Frizzle.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:22 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0