millercj Posted March 26, 2009 Share Posted March 26, 2009 I made a custom sort to reorder a multidimensional array of data pulled from a database. It seems to be sorting letters fine but doesn't work with numbers...which is what i need. If you can offer any suggestions they'd be appreciated. $sort_field = 5; function cmpi($a,$b) { global $sort_field; return strcmp($a[$sort_field], $b[$sort_field]); } usort($cartList, 'cmpi'); Quote Link to comment Share on other sites More sharing options...
sastro Posted March 26, 2009 Share Posted March 26, 2009 Can you post the result and what your expected result? Quote Link to comment Share on other sites More sharing options...
millercj Posted March 26, 2009 Author Share Posted March 26, 2009 Sure,I'll make a chart like thing Sort Order $cartList 1 36 3 40 4 43 5 46 6 50 Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 There are built in sorting functions for array in PHP. If multidimensional array: array_multisort() There are a lot of functions to check at: sort(), arsort(), asort(), ksort(), krsort(), natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort(). Does any of these work with what you need? Quote Link to comment Share on other sites More sharing options...
millercj Posted March 26, 2009 Author Share Posted March 26, 2009 well...to be honest i'm not a sorting expert. I've only ever used a basic sort (because of lack of knowledge on the others) and that will always and only sort the first column [0] of the array. I need the 6th [5] Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 Try this if this works. <?php // $ar is a multidimensional array array_multisort($ar[5], SORT_ASC, SORT_REGULAR); // if string only, use SORT_STRING; or SORT_NUMERIC if numeric as it suggests. ?> Jay Quote Link to comment Share on other sites More sharing options...
millercj Posted March 26, 2009 Author Share Posted March 26, 2009 array_multisort($cartList[5], SORT_ASC, SORT_REGULAR); Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag in /home/nutsab5/public_html/beta/phpscript/inlineCartView.php Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 oh my bad... tr this instead, we will be dissecting it though: <?php // given they have something like this $data = array(); $data[] = array('volume' => 67, 'edition' => 2); $data[] = array('volume' => 86, 'edition' => 1); $data[] = array('volume' => 85, 'edition' => 6); $data[] = array('volume' => 98, 'edition' => 2); $data[] = array('volume' => 86, 'edition' => 6); $data[] = array('volume' => 67, 'edition' => 7); // Obtain a list of columns foreach ($data as $key => $row) { $volume[$key] = $row['volume']; $edition[$key] = $row['edition']; } // Sort the data with volume descending, edition ascending // Add $data as the last parameter, to sort by the common key array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data); ?> can check this out? They should have common keys here... you can have an idea from here. Quote Link to comment Share on other sites More sharing options...
millercj Posted March 26, 2009 Author Share Posted March 26, 2009 this is what I did...no errors but still not sorting it right though foreach ($cartList as $key => $row) { $name[$key] = $row[0]; $price[$key] = $row[1]; $qty[$key] = $row[2]; $sizedesc[$key] = $row[3]; $image[$key] = $row[4]; $date[$key] = $row[5]; } array_multisort($date, SORT_DESC, $cartList); Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 this one really works fine: <?php // given they have something like this $data = array(); $data[] = array(0 => 67, 1 => 2); $data[] = array(0 => 86, 1 => ; $data[] = array(0 => 85, 1 => 6); $data[] = array(0 => 98, 1 => 2); $data[] = array(0 => 86, 1 => 6); $data[] = array(0 => 67, 1 => 7); // Obtain a list of columns foreach ($data as $key => $row) { $volume[$key] = $row[0]; $edition[$key] = $row[1]; } // Sort the data with volume descending, edition ascending // Add $data as the last parameter, to sort by the common key array_multisort($volume, SORT_DESC, SORT_REGULAR, $edition, SORT_ASC, $data); print_r($data); ?> may i know the structure of your array? Quote Link to comment Share on other sites More sharing options...
millercj Posted March 26, 2009 Author Share Posted March 26, 2009 <?PHP $cartList[$i] [0] = $nextresult['name']; $cartList[$i] [1] = $nextresult['price']; $cartList[$i] [2] = $nextresult['QTY']; $cartList[$i] [3] = $nextresult['sizeDesc']; $cartList[$i] [4] = $nextresult['image']; $cartList[$i] [5] = $nextresult['date']; ?> Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 so I presume you can d o something like this: <?php // all other code... given $resource is the result of mysql_query() $cartList = array(); while($nextresult = mysql_fetch_assoc($resource)) { $car = array(); $car[0] = $nextresult['name']; $car[1] = $nextresult['price']; $car[2] = $nextresult['QTY']; $car[3] = $nextresult['sizeDesc']; $car[4] = $nextresult['image']; $car[5] = $nextresult['date']; $cartList[] = $car; } foreach($cartList as $key => $row) { $name[$key] = $row[0]; $price[$key] = $row[1]; $qty[$key] = $row[2]; $sizedesc[$key] = $row[3]; $image[$key] = $row[4]; $date[$key] = $row[5]; } array_multisort($date, SORT_DESC, SORT_REGULAR, $cartList);?> one question though, you are sorting it by date right? I don't think it is really necessary to sort it again. And I think the problem you had is because you are sorting it with date... which I guess you need to make a custom one for that... except you add another field of date which is in unix timestamp and sort it with that as numeric. Apart from that, I still suggest not to sort it in PHP since you can sort it in SQL with date and knowing that changing the title doesn't really matter much with that, since you said you want to sort it with the sixth column. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.