NYCPHP Meetup

NYPHP.org

[nycphp-talk] How to sort a multi-dimensional array by a given key?

Scott Trudeau strudeau at umich.edu
Mon May 7 13:48:36 EDT 2007


Dan's solution is slightly more compact and a bit easier to generalize
if you're doing different kinds of sorts, but it's the same basic
idea.

Scott

On 5/7/07, Allen Shaw <ashaw at polymerdb.org> wrote:
> Wish I could answer that for you, Tim.  I've always done it this way and
> hadn't thought of uasort() before Scott and Dan mentioned it.  Hopefully
> they'll be able to comment on it, because I'm curious, too.
>
> - Allen
>
> Timothy Boyden wrote:
> > Scott's solution worked for me, but is there any performance/security
> > benefit from Allen's suggestion?
> >
> > -Tim
> >
> > -----Original Message-----
> > From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org]
> > On Behalf Of Allen Shaw
> > Sent: Monday, May 07, 2007 11:58 AM
> > To: NYPHP Talk
> > Subject: Re: [nycphp-talk] How to sort a multi-dimensional array by a
> > given key?
> >
> > Hi Tim,
> >
> > array_multisort() should do the trick.  See the manual
> > (http://php.net/array_multisort) for some examples ("Example 256.
> > Sorting database results") much like what you're describing.
> >
> > Essentially, you'll have to loop through the array to create another
> > array something like:
> >       $sortDistance['SCID1']=>20
> >       $sortDistance['SCID2']=>10
> > and then call:
> >       array_multisort($franchises, $sortDistance);
> >
> >
> >
> > - Allen
> >
> >
> > Timothy Boyden wrote:
> >
> >>Hi All,
> >>
> >>I have a multi-dimensional array such as:
> >>
> >>$franchises['SCID1'] = SCNYC
> >>$franchises['SCID1']['BusinessName'] = SuperCoups of NY City
> >>$franchises['SCID1']['DistanceFromCustomer'] = 20
> >>$franchises['SCID2'] = SCBUFF
> >>$franchises['SCID2']['BusinessName'] = SuperCoups of Buffalo
> >>$franchises['SCID2']['DistanceFromCustomer'] = 10
> >>
> >>How can I sort this array so the businesses are sorted by the
> >>DistanceFromCustomer key?
> >>
> >>Thanks for the help in advance,
> >>
> >>Tim
> >>
> >>---------------------------
> >>
> >>
> >>  Timothy Boyden
> >>
> >>/Network Administrator/
> >>
> >>tboyden at supercoups.com <mailto:tboyden at supercoups.com>
> >>
> >>*SuperCoups^(r) *
> >>
> >>**
> >>
> >>350 Revolutionary Drive | E. Taunton, MA 02718
> >>
> >>508-977-2034  | www.supercoups.com <http://www.supercoups.com/>
> >>
> >>
> >>
> >>We Support Alex's Lemonade Stand Foundation,
> >>
> >>"Fighting Childhood Cancer One Cup At A Time"
> >>
> >>Donations Accepted at: www.firstgiving.com/SuperCoups
> >><http://www.firstgiving.com/supercoups>
> >>
> >>---------------------------
> >>
> >>Local Coupons. Super Savings.^(r)
> >>
> >>
> >>
> >>
> >>
> >
> > ------------------------------------------------------------------------
> >
> >>_______________________________________________
> >>New York PHP Community Talk Mailing List
> >>http://lists.nyphp.org/mailman/listinfo/talk
> >>
> >>NYPHPCon 2006 Presentations Online
> >>http://www.nyphpcon.com
> >>
> >>Show Your Participation in New York PHP
> >>http://www.nyphp.org/show_participation.php
> >
> >
> >
>
>
> --
> Allen Shaw
> Polymer (http://polymerdb.org)
> slidePresenter (http://slides.sourceforge.net)
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>



More information about the talk mailing list