Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    e21
    e21 is offline
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation sorting multi-dimensional arrays

    I really hope someone can shed some light and help me out with this. I may even be able to pay for your time if you contact me privately.

    I have a ongoing website project that is querying a database for a very large multi dimenstional array. Within this array is a COLOR and SIZE. I am pulling the color out in order from the mysql query. This works fine. However, one row of data in the db is out of order for the SIZE. The SIZE 6 comes after the size 14. All other sizes are in order. This SIZE field is not an integer because the size could be text too (depending on the item being queried). Within a single item, the sizes are either number or Letter, but never both at the same time.

    Anyways, I need a great way to sort this out so that sizes are sorted after the color is sorted. I have tried a few nasort, usort, array_multisort, but none of them efficiently work because php thinks that size 10 comes before size 6.

    Can anyone here help out?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I would choose to use only numerical data for the size, then you're sql can do the work for you.
    The problem is simply string comparisons. To get around this, you'll need to write you're own code and use usort to make use of it.
    I don't know what you're array's look like so I can only speculate.
    PHP Code:
    function cmp($a$b)
    {
        
    $result 0;
        
    $tmpIntA null;
        
    $tmpIntB null;
        if (!
    is_array($a) || !is_array($b) || !isset($a['size']) || isset($b['size']))
        {
            throw new 
    Exception('One of the entries does not contain the correct data!');
        }
        
    $tmpIntA int_val($a['size']);
        
    $tmpIntB int_val($b['size']);
        if (
    $tmpIntA == $a['size'] && $tmpIntB == $b['size'])
        {
            
    $result $tmpIntA $tmpIntB;
        }
        else
        {
            
    $result strcmp($a['size'], $b['size']);
        }
        return 
    $result;
    }
    usort($aYourStack'cmp'); 
    Either way you're going to have problems since you're mixing strings with numbers - comparing (string(10) with string(small)) is tricky. The above cmp function trys to cast you're input to numbers, compares the numbers with the original strings to see if they are considered the same, and if they are it compares based on integer math. Otherwise it does a string comparison.
    You're best bet is to simply use numbers. No clue if the above actually works though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    e21
    e21 is offline
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Array[][COLOR][SIZE][PRICE][BARCODE][etc][etc][etc]

    There are over 10 of these Array[0]..Array[10]

    Array is setup to spit out a dropdown for the customer to select sub item options


    I have seen some code snipets similar to what you have posted, but I have not enough understanding of what it is and how it works to fit it to my situation.

    There is no way I can change the Size to only a #. In the database SIZE has a max value of 43 characters when the product is not a shoe.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The code above will probably work for what you're doing, you should try it out. The only thing you'll need to change is the case for the size. Just query to order by the colour, then take the values provided and run them through the usort. Post back with you're results.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Tags for this Thread

    Posting Permissions

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