Re: Sorting multi-dimensional arrays
| From: | Joe Stump | Date: | Tue, 31 Oct 2000 09:59:50 +0000 |
| Subject: | Re: Sorting multi-dimensional arrays | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-22894@lists.php.net to get a copy of this message | ||
Ummm (total stab in the dark - I've never used this ...)
<?
$arrayA = array(1,4,5,2,3);
$arrayB = array(
columnB => array(96,23,2,5,6),
columnA => array(1,5,21,4,6));
array_multisort(&$arrayA,SORT_ASC);
array_multisort(&$arrayB,SORT_ASC);
while(list($key,$val) = each($arrayA)){
echo $key.' ==> '.$val."\n";
}
while(list($key,$val) = each($arrayB)){
echo $key.' ==> '.$val."\n";
}
?>
Pretty cool - it sorted the second on like this:
columnA ==> Array
columnB ==> Array
They were sorted :O)
--Joe
On Tue, Oct 31, 2000 at 09:24:19AM +0100, Tobias H. Michaelsen wrote:
> "Joe Stump" <joe@earth.care2.com> wrote in message
> news:20001030100702.C31604@zorka.com...
> > http://www.php.net/manual/function.array-multisort.php
> >
>
> Yes, i have seen that one, but how would you use it to sort by a specific
> column? The manual does say that it "resembles the functionality of SQL
> ORDER BY clause", and that is exactly what i want, but i cant see how -- the
> examples all sorts the content the sub-arrays, and that is not what i want.
>
> -Tobias-
>
> > --Joe
> >
> > On Mon, Oct 30, 2000 at 03:52:31PM +0100, Tobias H. Michaelsen wrote:
> > > Is there a way to sort a multi-dimensional array by a specified collumn?
> > >
> > > ex. if i want to sort by the 3rd row:
> > >
> > > /===
> > > | $ar = array( array( 23, 'kjhsdfjhd', 952106215, 'jkhg' ),
> > > | array( 24, 'zmncjdflf', 952103423, 'fgfg' ) );
> > > | sort_multi_array_by_col( $ar, 2 );
> > > \===
> > >
> > > and then i get:
> > >
> > > $ar[0] => [ 24, 'zmncjdflf', 952103423, 'fgfg' ]
> > > $ar[1] => [ 23, 'kjhsdfjhd', 952106215, 'jkhg' ]
> > >
> > > Or perhaps by key-name:
> > >
> > > /===
> > > | $ar = array( array( "name" => "John", "telephone"
> > > => "555-4321" ),
> > > | array( "name" => "Bob", "telephone"
> > > => "555-1234" ) );
> > > | sort_multi_array_by_col( $ar, "telephone" );
> > > \===
> > >
> > > -Tobias-
> > >
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
================================================================================
= Joe Stump joestump98@yahoo.com http://www.miester.org =
================================================================================
"Real programmers don't comment their code.
It was hard to write, it should be hard to understand."