djkilgus Posted November 25, 2011 Share Posted November 25, 2011 I have a multidimensional array that I want to sort by "price" then by "date". Here is an example of the array: $productArray = array ( array ("Bus", "1/1/2010", 15.99), array ("Train", "2/1/2010", 14.99), array ("Car", "3/1/2010", 18.00), array ("Plane", "3/1/2010", 15.99), array ("Bike", "3/1/2010", 9.99), array ("Truck", "1/1/2010", 19.99) ); //sort by date function sortElement1Asc($x, $y) { if ( $x[1] == $y[1] ) return 0; else if ( $x[1] < $y[1] ) return -1; else return 1; } //sort by price function sortElement2Asc($x, $y) { if ( strtotime($x[2]) == strtotime($y[2]) ) return 0; else if ( strtotime($x[2]) < strtotime($y[2]) ) return -1; else return 1; } usort($productArray, 'sortElement1Asc'); usort($productArray, 'sortElement2Asc'); Unfortunately this is just sorting by price (or whatever i sort last). Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/251796-multidimensional-array-multiple-sort/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2011 Share Posted November 25, 2011 The comment above each function doesn't match what the code is doing in each function, which also results in the wrong array element number being accessed by the code in each function. Quote Link to comment https://forums.phpfreaks.com/topic/251796-multidimensional-array-multiple-sort/#findComment-1291172 Share on other sites More sharing options...
djkilgus Posted November 25, 2011 Author Share Posted November 25, 2011 PFMaBiSmAd, thanks for the response. The individual sort functions are working fine. If I comment out usort($productArray, 'sortElement1Asc'); and run the code it sorts by price ascending and vice versa with the other usort line. I just can't get a sort by date, sort by price to work. Quote Link to comment https://forums.phpfreaks.com/topic/251796-multidimensional-array-multiple-sort/#findComment-1291173 Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2011 Share Posted November 25, 2011 I recommend that you play computer and proofread your code and comments in your code to see if it is logically doing what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/251796-multidimensional-array-multiple-sort/#findComment-1291174 Share on other sites More sharing options...
kicken Posted November 26, 2011 Share Posted November 26, 2011 Your two sorting functions are completely separate and have no relation to each other. When you call usort($productArray, 'sortElement1Asc'); your sorting the array by the date. Next, when you call usort($productArray, 'sortElement2Asc'); your sorting the array by the price, but in the process you completely overwrite the previous sorting. This second call is a completely separate process with no relation to the previous sort. What you need to do to sort by both is create one single function which will do the sorting you need. First, compare the price. If they differ, return -1 or 1 accordingly. If they are equal, move on to your second condition on the date. Compare the dates and return -1, 0, or 1 as appropriate. Quote Link to comment https://forums.phpfreaks.com/topic/251796-multidimensional-array-multiple-sort/#findComment-1291273 Share on other sites More sharing options...
djkilgus Posted November 27, 2011 Author Share Posted November 27, 2011 kicken, Thank you for the constructive reply. Thanks to you I figured it out. For anyone who is interested here is the correct (single) sort function: function sortByDateThenByPrice($x, $y) { if ( $x[1] == $y[1] ) if ( $x[2] == $y[2] ) return 0; else if ( $x[2] < $y[2] ) return -1; else return 1; else if ( $x[1] < $y[1] ) return -1; else return 1; } usort($productArray, 'sortByDateThenByPrice'); Quote Link to comment https://forums.phpfreaks.com/topic/251796-multidimensional-array-multiple-sort/#findComment-1291466 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.