SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Southampton, Essex, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting a multi dimensional array

    Hi guys,

    I'm having a bit of a problem sorting a multi-dimensional array with PHP. The array has the following structure:

    Code:
    Array (array(5)) {
    |    [0] (array(3)) {
    |    |    ['prod_name'] (string) = "card 12"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "17"
    |    }
    |    [1] (array(3)) {
    |    |    ['prod_name'] (string) = "card 14"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "18"
    |    }
    |    [2] (array(3)) {
    |    |    ['prod_name'] (string) = "card 16"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "19"
    |    }
    }
    I want to sort the array by "prod_name". I've tried nearly all the native PHP array sorting functions I can find including array_multisort() but to no avail.

    I've also tried restructuring the array thus:

    Code:
    Array (array(5)) {
    |    ['card 12'] (array(2)) {
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "17"
    |    }
    |    ['card 14'] (array(2)) {
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "18" |    }
    |    ['card 16'] (array(2)) {
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "19"
    |    }
    }
    and then sorting the keys but again, no dice...

    Any ideas?

    Thanks in advance for any help.

    Cheers,
    Terry
    Terry Morgan,
    Lead developer / director,
    Central Media UK Ltd.
    w: Southampton web design and PHP development

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try usort

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Southampton, Essex, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    Try usort
    Using usort() on the following array:

    Code:
    Array (array(5)) {
    |    ['0'] (array(3)) {
    |    |    ['prod_name'] (string) = "card 12"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "17"
    |    }
    |    ['1'] (array(3)) {
    |    |    ['prod_name'] (string) = "card 13"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "18"
    |    }
    |    ['2'] (array(3)) {
    |    |    ['prod_name'] (string) = "Niec xmas card"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "26"
    |    }
    |    ['3'] (array(3)) {
    |    |    ['prod_name'] (string) = "test xmas gift"
    |    |    ['prod_type'] (string) = "2"
    |    |    ['prod_id'] (string) = "1"
    |    }
    |    ['4'] (array(3)) {
    |    |    ['prod_name'] (string) = "a very nice gift"
    |    |    ['prod_type'] (string) = "2"
    |    |    ['prod_id'] (string) = "2"
    |    }
    }
    using the following code:

    Code:
    function prod_cmp($a, $b)
    {
        return strcmp($a["prod_name"], $b["prod_name"]);
    }
    
    usort($_arrProducts, "prod_cmp");
    produces the following results

    Code:
    Array (array(5)) {
    |    ['0'] (array(3)) {
    |    |    ['prod_name'] (string) = "Niec xmas card"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "26"
    |    }
    |    ['1'] (array(3)) {
    |    |    ['prod_name'] (string) = "a very nice gift"
    |    |    ['prod_type'] (string) = "2"
    |    |    ['prod_id'] (string) = "2"
    |    }
    |    ['2'] (array(3)) {
    |    |    ['prod_name'] (string) = "card 12"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "17"
    |    }
    |    ['3'] (array(3)) {
    |    |    ['prod_name'] (string) = "card 13"
    |    |    ['prod_type'] (string) = "1"
    |    |    ['prod_id'] (string) = "18"
    |    }
    |    ['4'] (array(3)) {
    |    |    ['prod_name'] (string) = "test xmas gift"
    |    |    ['prod_type'] (string) = "2"
    |    |    ['prod_id'] (string) = "1"
    |    }
    }
    Not quite what I expected....

    I've tried uksort() too, with and without the prod_name as the key, neither works...

    Cheers,
    Terry
    Terry Morgan,
    Lead developer / director,
    Central Media UK Ltd.
    w: Southampton web design and PHP development

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    looks sorted to me, N is uppercase, the rest start with lower case and so they're sorted appropriately.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Southampton, Essex, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wonshikee View Post
    looks sorted to me, N is uppercase, the rest start with lower case and so they're sorted appropriately.
    Ah ok, I didn't realise that case made a difference!

    Many thanks, that explains a LOT lol
    Terry Morgan,
    Lead developer / director,
    Central Media UK Ltd.
    w: Southampton web design and PHP development


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
  •