473,387 Members | 1,540 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 Multi Dimensional Array.

I've been getting better at arrays but I got stumped. Ok pretty much I have an array as such.

[PHP]$row[0]
[0] =>why
[1] =>boo
[2] =>120

$row[1]
[0] =>etc
[1] =>hello
[2] =>12

$row[2]
[0] =>straw
[1] =>grape
[2] =>20

$row[3]
[0] =>apple
[1] =>orange
[2] =>30[/PHP]

so im using this:
[PHP]function cmp($a, $b)
{
return strcmp($a[2], $b[2]);
}

usort($row, "cmp");

while (list($key, $value) = each($row)) {
echo "\$row[$key]: " . $value[2] . "\n";
}
[/PHP]

and it semiworks, i think. except I get something like.
[PHP]

$row[0]
[0] =>etc
[1] =>hello
[2] =>12

$row[1]
[0] =>why
[1] =>boo
[2] =>120

$row[2]
[0] =>straw
[1] =>grape
[2] =>20

$row[3]
[0] =>apple
[1] =>orange
[2] =>30[/PHP]

When obviously I want something more like natsort, i think. pretty much:
[PHP]

$row[0]
[0] =>etc
[1] =>hello
[2] =>12

$row[1]
[0] =>straw
[1] =>grape
[2] =>20

$row[2]
[0] =>apple
[1] =>orange
[2] =>30

$row[3]
[0] =>why
[1] =>boo
[2] =>120[/PHP]

Any help plz?
Mar 12 '08 #1
12 1086
satas
82
Check this out:
http://www.php.net/manual/ru/function.sort.php#13839

There are at least 2 ways sorting multidimentional array. Try to implement those solutions by yourself or ask questions according to the code.
Mar 12 '08 #2
Wow! that worked beautifully thanx for the reference.
Mar 12 '08 #3
satas
82
Wow! that worked beautifully thanx for the reference.
You are welcome.
php.net is the site number one for php programmers.
Mar 12 '08 #4
Markus
6,050 Expert 4TB
You are welcome.
php.net is the site number one for php programmers.
Really?!
This is news to me!
Mar 12 '08 #5
TheServant
1,168 Expert 1GB
php.net is the site number one for php programmers.
TheScripts is where it's at :P

Useful link, thanks for that satas.
Mar 12 '08 #6
ronverdonk
4,258 Expert 4TB
TheScripts is where it's at :P

Useful link, thanks for that satas.
So at the Scripts they can show you the relevant chapters in the PHP documentation!

Ronald
Mar 12 '08 #7
satas
82
TheScripts is where it's at :P
Would you be so kind to explain me what does this means?
Mar 13 '08 #8
satas
82
TheScripts is where it's at :P
I guess i get it :)
(trying to get my english better)
Well, i don't agree. When you have php-related query, first of all you should have a look at the documentation (php.net in that case) And after that ask community questions. As for me it sounds like "Argh, it is so boring to spend hours reading the manual, writing the code... maybe someone could do it instead of me?" Wrong way, i think.
Mar 13 '08 #9
Markus
6,050 Expert 4TB
I guess i get it :)
(trying to get my english better)
Well, i don't agree. When you have php-related query, first of all you should have a look at the documentation (php.net in that case) And after that ask community questions. As for me it sounds like "Argh, it is so boring to spend hours reading the manual, writing the code... maybe someone could do it instead of me?" Wrong way, i think.
People will do what they do and normally that's not to get sidetracked in reading but to get fist deep in practical.

Plus, if people read the manual, what help would i be?
Mar 13 '08 #10
satas
82
People will do what they do and normally that's not to get sidetracked in reading but to get fist deep in practical.

Plus, if people read the manual, what help would i be?
Manual was written by people and for people.
You'll become good programmer as fast as you'll start thinking about the problem, not asking someone to solve it.

Don't know who the author, but i like it :
'Ever tried? Ever failed? Try again. Fail again. Fail better'
Mar 13 '08 #11
Markus
6,050 Expert 4TB
Manual was written by people and for people.
Really? I thought it was aliens!
Mar 13 '08 #12
ronverdonk
4,258 Expert 4TB
A lot of people just start programming by downloading some code, trying to change it and, when it does not go the way they want it, come to this forum. They just lack the necessary theory and general knowledge. That is why I point a lot of people to the standard public documentation for PHP and MySQL.

See my signature.

Ronald
Mar 14 '08 #13

Sign in to post your reply or Sign up for a free account.

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: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
4
by: Robert P. | last post by:
I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.