SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dimis283 View Post
    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. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function cmp($a$b) {
        if (
    $a[1] == $b[1]) {
            return 
    0;
        }
        return (
    $a[1] < $b[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. #4
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why is 118 not [3][1]? If those indexes are right, there's no structure here at all...

  6. #6
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was a typing error,

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Then the code I gave you should work. Try it out.


Bookmarks

Posting Permissions

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