Sorting a Two Dimensional Array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi. I have an array, thus:

Array (
[0] => Array ( [Freq] => 28 [Name] => Tomato )
[1] => Array ( [Freq] => 4 [Name] => Banana )
[2] => Array ( [Freq] => 54 [Name] => Orange )
[3] => Array ( [Freq] => 24 [Name] => Eggplant )
[4] => Array ( [Freq] => 5 [Name] => Courgette )
[5] => Array ( [Freq] => 4 [Name] => Apple )
)

I would like to be able to sort it either by the Frequency or the Name
of the fruit or vegetable.

I just can't seem to get it to work. I'm sorry - I realise that this is
probably really easy but my brain isn't working today and the examples
on php.net seem incomprehensible to me.

Many thanks, in anticipation

Steve

Re: Sorting a Two Dimensional Array

StevePBurgess@gmail.com wrote:

// \$key = 'Freq';
\$key = 'Name';

function callback(\$a, \$b) {
global \$key;
if (\$a[\$key] == \$b[\$key]) return 0;
return \$a[\$key] > \$b[\$key] ? 1 : -1;
}
uasort(\$array, 'callback');

JW

Re: Sorting a Two Dimensional Array

Janwillem Borleffs wrote:

Brilliant - many thanks.

Steve

Re: Sorting a Two Dimensional Array (2)

<comp.lang.php>
<>
<26 Jun 2006 14:50:17 -0700>

Can anything like the following be used for 2 dimentional arrays ? .

\$demo=[1,1]="blah";
\$demo=[1,2]="blah";

--
www.emailuser.co.uk/?name=KRUSTOV

Re: Sorting a Two Dimensional Array (2)

<comp.lang.php>
<Krustov>
<Tue, 27 Jun 2006 12:40:46 +0100>

No answers provided for this question - so assume there isnt .

--
www.emailuser.co.uk/?name=KRUSTOV

Re: Sorting a Two Dimensional Array (2)

Krustov wrote:

First of all, you need to give people a chance to answer - at least 24 hours.
We're all volunteers and some only check in once a day.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Sorting a Two Dimensional Array (2)

\$xx=array(array(1,2),array(3,4));
echo \$xx[0][0]; // prints 1
echo \$xx[1][1]; // prints 4

Krustov wrote:

Re: Sorting a Two Dimensional Array (2)

ImOk wrote:

Which also follows you can do this:

\$xx[0][0]="X";
echo \$xx[0][0];

Re: Sorting a Two Dimensional Array (2)

<comp.lang.php>
<ImOk>
<27 Jun 2006 18:31:17 -0700>

Yeah that looks fine - thanks .

--
www.emailuser.co.uk/?name=KRUSTOV