piznac Posted October 27, 2012 Share Posted October 27, 2012 I have several arrays similar to this one: [2] => Array ( [0] => Array ( [player] => piznac [playerId] => 1 [positionRow] => 0 ) [1] => Array ( [player] => [playerId] => [positionRow] => ) [2] => Array ( [player] => Player 3 [playerId] => 3 [positionRow] => 1 ) [3] => Array ( [player] => [playerId] => [positionRow] => ) ) How would I re-sort this array by the 'positionRow' key with still keeping the empty arrays. Something like this: [2] => Array ( [0] => Array ( [player] => piznac [playerId] => 1 [positionRow] => 0 ) [1] => Array ( [player] => Player 3 [playerId] => 3 [positionRow] => 1 ) [2] => Array ( [player] => [playerId] => [positionRow] => ) [3] => Array ( [player] => [playerId] => [positionRow] => ) ) Quote Link to comment https://forums.phpfreaks.com/topic/269980-php-sort-associative-array-by-array-key/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2012 Share Posted October 27, 2012 You can do this in php using one of two different methods - 1) Extract the positionrow values and use array_multisort. See Example #3 at the following link -array_multisort 2) You can write a call-back function that compares the values and use usort or since this is probably data from a database query, why not just sort the data in the query? Quote Link to comment https://forums.phpfreaks.com/topic/269980-php-sort-associative-array-by-array-key/#findComment-1388142 Share on other sites More sharing options...
piznac Posted October 27, 2012 Author Share Posted October 27, 2012 or since this is probably data from a database query, why not just sort the data in the query? Geez such a simple solution and it never dawned on me,.. thanks a bunch man! Quote Link to comment https://forums.phpfreaks.com/topic/269980-php-sort-associative-array-by-array-key/#findComment-1388143 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.