SitePoint Sponsor

# Thread: sort a 2X2 array

1. ## sort a 2X2 array

I have an array with 2 dimensions, the first dimension has the name of a company, the second the hits of their website.
I want to sort the entire array (not each dimension) with the hits (numbers).
How can I do it?
Dimis

2. Originally Posted by dimis283
I have an array with 2 dimensions, the first dimension has the name of a company, the second the hits of their website.
I want to sort the entire array (not each dimension) with the hits (numbers).
How can I do it?
Dimis
Can you give an example of your array?
Because I'm wondering what the logic behind its structure is. What is the value of, let's say, \$array['companyname']['10']?

3. PHP Code:
``` function cmp(\$a, \$b) {    if (\$a[1] == \$b[1]) {        return 0;    }    return (\$a[1] < \$b[1]) ? -1 : 1;}\$a = array(  array('site1', 100),  array('site2', 200),  array('site3', 50));usort(\$a, "cmp");  ```
http://us.php.net/usort

If this isn't how your array is set up (your description is ambiguous), then you need to adjust the cmp function to match.

4. My array is
Code:
```\$data=array();
\$data[1][0]='name 1';\$data[1][1]=125;
\$data[2][0]='name 2';\$data[2][1]=188;
\$data[3][0]='name 3';\$data[3][1]=118;
...```

5. Why is 118 not [3][1]? If those indexes are right, there's no structure here at all...

6. It was a typing error,

7. Then the code I gave you should work. Try it out.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•