Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-10-2008, 10:30 PM   PM User | #1
e21
New to the CF scene

 
Join Date: Apr 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
e21 is an unknown quantity at this point
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?
e21 is offline   Reply With Quote
Old 09-10-2008, 10:43 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 09-10-2008, 10:49 PM   PM User | #3
e21
New to the CF scene

 
Join Date: Apr 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
e21 is an unknown quantity at this point
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.
e21 is offline   Reply With Quote
Old 09-10-2008, 11:00 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
array, multi, mysql, php, sort

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:41 PM.


Advertisement
Log in to turn off these ads.