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] => ) ) 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? 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! 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
Archived
This topic is now archived and is closed to further replies.