RE: [PHP] Need some help with array sorting
| From: | Will Steffen | Date: | Mon, 16 Sep 2002 09:37:22 +0000 |
| Subject: | RE: [PHP] Need some help with array sorting | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-116501@lists.php.net to get a copy of this message | ||
Thanks Timo
I looked at those examples in the manual (I always rtfm as much as
possible before I ask :-), but my poor brain couldn't quite make sense
of what the user defined function was sposed to do and which one of the
usersorts was going to do what I needed - your example has given me the
right clues to get me going - ta muchly.
-Will
> -----Original Message-----
> From: timo stamm [mailto:t.stamm@macnews.de]
> Sent: 14 September 2002 03:14 PM
> To: php-general@lists.php.net
> Cc: timo stamm
> Subject: Re: [PHP] Need some help with array sorting
>
>
> Hi Will,
>
>
> After completing an example for you, I had a glance at the php
> manual and saw that there already are perfect examples and
> descriptions!
> I can't explain any better. At least I won't for free. Anyhow,
> here is what I wrote:
>
>
> <?PHP
> // define multidim array
> $blocks[0] = array("param1"=>"4","param2"=>"some other
>
> param"); $blocks[1] =
> array("param1"=>"3","param2"=>"some
> other param"); $blocks[2] =
> array("param1"=>"6","param2"=>"some other
> param");
>
> echo ("order of blocks/param1 <i>before</i> custom
> sort:<br>"); for($i=0;$i<count($blocks);$i++){
> echo($blocks[$i]['param1'] ."<br>");
> }
>
> // define custom sort function:
> function customsort($a, $b) {
> if ($a['param1'] == $b['param1']) return 0;
> return ($a['param1'] > $b['param1']) ? 1 : -1;
> }
> // sort customized
> usort($blocks, "customsort");
>
> echo ("order of blocks/param1 <i>after</i> custom
> sort:<br>"); for($i=0;$i<count($blocks);$i++){
> echo($blocks[$i]['param1'] ."<br>");
> }
> ?>
>
>
> Timo
>
>
> Am Freitag den, 13. September 2002, um 10:07, schrieb Will Steffen:
>
> > Thanks Timo
> >
> > I figured if it was do-able it would be something like uasort,
> > or uksort
> > or one of those, but my geek muscles arent quite up to that
> > sort of flex
> > yet - any one mind giving me some clues as to how to get
> started using
> > these functions - the manual gets pretty vague in this area?
> >
> >
> > Thanks
> > Will
> >
> >> -----Original Message-----
> >> From: t.stamm@web.de [mailto:t.stamm@web.de]
> >> Sent: 13 September 2002 08:41 AM
> >> To: php-general@lists.php.net
> >> Cc: timo stamm; Will Steffen
> >> Subject: Re: [PHP] Need some help with array sorting
> >>
> >>
> >> Hi Will,
> >>
> >>
> >> You need a customized sort. Have a look at uasort().
> >>
> >>
> >> Timo
> >>
> >>
> >> Am Donnerstag den, 12. September 2002, um 13:32, schrieb Will
> >> Steffen:
> >>
> >>> Ahem *tap* *tap* is this thing on?
> >>>
> >>> Hi everyone, my name is Will and I've been addicted to php for 3
> >>> months and 21 days... *grin*
> >>>
> >>> I'm hitting a bit of a brick wall in a current project -
> >> the answer is
> >>> probably quite simple, but I'm still newish to php and coding in
> >>> general so bear with me:
> >>>
> >>> I have a multidim array which looks something like :
> >>>
> >>> $blocks[0] =
> >>> array("param1"=>"4","param2"=>"some other param")
> >>> $blocks[1] =
> >>> array("param1"=>"3","param2"=>"some other param")
> >>> $blocks[2] =
> >>> array("param1"=>"6","param2"=>"some other param")
> >>>
> >>> And so forth
> >>>
> >>> Fairly straight forward you say, my problem is that
> elements can be
> >>> inserted into the blocks array in no particular order and
> I need to
> >>> sort at the top level using the values in param1 - so my
> >> output should
> >>> be:
> >>>
> >>> $sorted_blocks[0] =
> >>> array("param1"=>"3","param2"=>"some
> >> other param")
> >>> $sorted_blocks[1] =
> >>> array("param1"=>"4","param2"=>"some
> >> other param")
> >>> $sorted_blocks[2] =
> >>> array("param1"=>"6","param2"=>"some
> >> other param")
> >>>
> >>> I've had a long hard look at the manual and all those
> obscure array
> >>> manipulation goodies (ksort, multisort, etc) and done some
> >> hunting on
> >>> phpbuilder and the like, but turned up zip - either I'm not
> >>> understanding the array functions properly, or this can't
> >> be done, or
> >>> there's a nifty trick for doing it which I havent figured out?
> >>>
> >>> Ideally I'd like to be populating blocks using an sql query
> >> so I could
> >>> do an order by or some such upfront, but this world is
> anything but
> >>> ideal.
> >>>
> >>> Any1 got 2c for me?
> >>>
> >>>
> >>> Thanks in advance
> >>> Will
> >>>
> >>>
> >>>
> >>>
> >>> --
> >>> PHP General Mailing List (http://www.php.net/)
> >>> To unsubscribe, visit:
> >>> http://www.php.net/unsub.php
> >>>
> >>
> >>
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>