424,948 Members | 816 Online
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

# Sorting a Two Dimensional Array

 P: n/a 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 Jun 26 '06 #1
8 Replies

 P: n/a St***********@gmail.com wrote: 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. // \$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 Jun 26 '06 #2

 P: n/a Janwillem Borleffs 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'); Brilliant - many thanks. Steve Jun 26 '06 #3

 P: n/a <> <26 Jun 2006 14:50:17 -0700> <11**********************@b68g2000cwa.googlegroups .com> Brilliant - many thanks. OP question answered . 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 Jun 27 '06 #4

 P: n/a Can anything like the following be used for 2 dimentional arrays ? . \$demo=[1,1]="blah"; \$demo=[1,2]="blah"; No answers provided for this question - so assume there isnt . -- www.emailuser.co.uk/?name=KRUSTOV Jun 27 '06 #5

 P: n/a Krustov wrote: Can anything like the following be used for 2 dimentional arrays ? .\$demo=[1,1]="blah";\$demo=[1,2]="blah"; No answers provided for this question - so assume there isnt . 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. To answer your question, no. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. js*******@attglobal.net ================== Jun 28 '06 #6

 P: n/a \$xx=array(array(1,2),array(3,4)); echo \$xx[0][0]; // prints 1 echo \$xx[1][1]; // prints 4 Krustov wrote: Can anything like the following be used for 2 dimentional arrays ? . \$demo=[1,1]="blah"; \$demo=[1,2]="blah"; No answers provided for this question - so assume there isnt . -- www.emailuser.co.uk/?name=KRUSTOV Jun 28 '06 #7

 P: n/a ImOk wrote: \$xx=array(array(1,2),array(3,4)); echo \$xx[0][0]; // prints 1 echo \$xx[1][1]; // prints 4 Which also follows you can do this: \$xx[0][0]="X"; echo \$xx[0][0]; Jun 28 '06 #8

 P: n/a <27 Jun 2006 18:31:17 -0700> <11**********************@x69g2000cwx.googlegroups .com> \$xx[0][0]="X"; echo \$xx[0][0]; Yeah that looks fine - thanks . -- www.emailuser.co.uk/?name=KRUSTOV Jun 28 '06 #9

### This discussion thread is closed

Replies have been disabled for this discussion.