SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    sort array by key.

    I've haven't found an array function that will let me sort this result by the ['item_name'] key. If that function exists, what is it please? If not, how should I be thinking to sort these rows by ['item_name']?

    Thanks for your time.

    Here's the vardump() of my sample array which is called $result:

    array(4) { [0]=> string(1) "4" ["id"]=> string(1) "4" [1]=> string(28) "Superboy and His Dog Krypto " ["item_name"]=> string(28) "Superboy and His Dog Krypto " }

    array(4) { [0]=> string(1) "5" ["id"]=> string(1) "5" [1]=> string(12) "Wonder Woman" ["item_name"]=> string(12) "Wonder Woman" }

    array(4) { [0]=> string(1) "6" ["id"]=> string(1) "6" [1]=> string(16) "Seaview 8-Window" ["item_name"]=> string(16) "Seaview 8-Window" }

    array(4) { [0]=> string(1) "7" ["id"]=> string(1) "7" [1]=> string(31) "Creature from the Black Lagoon " ["item_name"]=> string(31) "Creature from the Black Lagoon " }

  2. #2
    SitePoint Enthusiast Strider64's Avatar
    Join Date
    Aug 2012
    Posts
    34
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    /****
     *  Find more info here: http://us2.php.net/manual/en/function.uasort.php
     *  I didn't setup the array exactly as you had it but I think you can get
     *  the idea behind it. By the way it's sorted by array indices.
     */

    $result = array(
        
    => array("id" => 4"item_name" => "Superboy and His Dog Kryto"),
        
    => array("id" => 5"item_name" => "Wonder Woman"),
        
    => array("id" => 6"item_name" => "Seaview 8-Window"),
        
    => array("id" => 7"item_name" => "Creature from the Black Lagoon")
    );

    // Name sorting function:
    function name_sort($x$y) {
        return 
    strcasecmp($x['item_name'], $y['item_name']);
    }

    // Print the array as is:
    echo '<h2>Array As Is</h2><pre>' print_r($result1) . '</pre>';

    // Sort by name:    
    uasort($result'name_sort');
    echo 
    '<h2> Array Sorted by "item_name"</h2><pre>' print_r($result1) . '</pre>';

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    How would I create a new array in ['item_name'] order based on your original array?

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Copy the array first.
    - Robert

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK. I didn't realize uasort() rearranged the array.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for you help QMonkey
    and especially for your's Strider64 that was a lot of work.


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
  •