473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sorting a Two Dimensional Array

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 1940
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

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
<comp.lang.php>
<>
<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
<comp.lang.php>
<Krustov>
<Tue, 27 Jun 2006 12:40:46 +0100>
<MP************************@news.newsreader.com>
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
Krustov wrote:
<comp.lang.php>
<Krustov>
<Tue, 27 Jun 2006 12:40:46 +0100>
<MP************************@news.newsreader.com>
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

$xx=array(array(1,2),array(3,4));
echo $xx[0][0]; // prints 1
echo $xx[1][1]; // prints 4
Krustov wrote:
<comp.lang.php>
<Krustov>
<Tue, 27 Jun 2006 12:40:46 +0100>
<MP************************@news.newsreader.com>
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

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
<comp.lang.php>
<ImOk>
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
5
by: Eclectic | last post by:
Hey all, I have been using usort to sort my multi dimensional arrays ... function cmp($a, $b){ if($a == $b){ return 0; } return ($a < $b) ? -1 : 1; }
2
by: D. Roshani | last post by:
Hello ! I wonder if any one can help me to create a cosomize sorting order (as Macro or added small program in c++ or c# which does this work) in a Access Database contaning one table only words...
4
by: Todd | last post by:
I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are...
7
by: Karin Jensen | last post by:
Hi I am running a PHP program that connects to an Access 2000 database via ODBC: $results = odbc_exec($connection_id, $sql_select); Is it possible to sort the contents of $results? I wish to...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
10
by: Roy Gourgi | last post by:
Hi, How would I sort an array? TIA Roy
0
by: Sick | last post by:
Hey, The Array.Sort function only applies to one-dimension arrays. Is there a solution for sorting a two-dimensional array, preferably *without* the usage of additional controls such as...
5
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not...
1
by: Totti | last post by:
Hi everyone, I am a newbie to javascript learning it for 2 months now, i am trying to make a sorter who will read english words from the one textbox or datafile and store them in a 2 dimensional...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.