Destramic Posted October 7, 2011 Share Posted October 7, 2011 i have an array from a database that looks like this (below) but what i want is for the array to be sorted assedningly by the start position so the team with start position 1 will be 1st and so on...if anyone can help me on what i should do please...thank you Array ( [1] => Array ( [team_name] => A Team [team_nationality] => British [start_position] => 2 ) [2] => Array ( [team_name] => B Team [team_nationality] => British [start_position] => 1 ) [3] => Array ( [team_name] => C Team [team_nationality] => British [start_position] => 4 ) [4] => Array ( [team_name] => B Team [team_nationality] => British [start_position] => 3 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/248625-php-array-sorting/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 7, 2011 Share Posted October 7, 2011 Use the following in your query statement - ORDER BY start_position Quote Link to comment https://forums.phpfreaks.com/topic/248625-php-array-sorting/#findComment-1276780 Share on other sites More sharing options...
Destramic Posted October 7, 2011 Author Share Posted October 7, 2011 is there a way to sort the array to please? Quote Link to comment https://forums.phpfreaks.com/topic/248625-php-array-sorting/#findComment-1276783 Share on other sites More sharing options...
AyKay47 Posted October 7, 2011 Share Posted October 7, 2011 perhaps look into using array_multisort Quote Link to comment https://forums.phpfreaks.com/topic/248625-php-array-sorting/#findComment-1276787 Share on other sites More sharing options...
ManiacDan Posted October 7, 2011 Share Posted October 7, 2011 The correct way to do it is by sorting the query. If you can't sort the query for some reason, then array_multisort or usort are the functions you need. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/248625-php-array-sorting/#findComment-1276799 Share on other sites More sharing options...
Destramic Posted October 7, 2011 Author Share Posted October 7, 2011 thanks guys...one last thing...how can i convert a php array to a javascript array please? Quote Link to comment https://forums.phpfreaks.com/topic/248625-php-array-sorting/#findComment-1276895 Share on other sites More sharing options...
Psycho Posted October 7, 2011 Share Posted October 7, 2011 thanks guys...one last thing...how can i convert a php array to a javascript array please? Create a foreach loop to "write out" the array values into the page in the format you want for the JavaScript array. YOU have to decide how you want the JavaScript array to be structured. JS arrays do not allowed named keys. You can use named keys - but then it is an "object" in JS and not an array - which changes how you can use it. Quote Link to comment https://forums.phpfreaks.com/topic/248625-php-array-sorting/#findComment-1276904 Share on other sites More sharing options...
ManiacDan Posted October 7, 2011 Share Posted October 7, 2011 Javascript supports json encoding, but like mj said it doesn't support named keys. Your "team name" and "team nationality" will turn into 0 and 1, respectively. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/248625-php-array-sorting/#findComment-1276992 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.