Zugzwangle Posted February 8, 2013 Share Posted February 8, 2013 Hi there all, Thank you for your suggestions and help in anticipation of your help!! I have 2x arrays. The first is an array of the totals for each round. The second is an array split into weeks (the weeks being the first level keys. I would like to arrange all of the second subarray team names, in order of the teamnames of the first array. so in order of [team3], [team5], [team1], [team2] and [team4].. I hope you follow what I mean.. Here are the first and second arrays!! First array (Total of rounds) Array ( [Team3] => Array ( [brd_points] => 14 [ttl_points] => 5 [games] => 3 ) [Team5] => Array ( [brd_points] => 12 [ttl_points] => 3 [games] => 2 ) [Team1] => Array ( [brd_points] => 7 [ttl_points] => 3 [games] => 2 ) [Team2] => Array ( [brd_points] => 16 [ttl_points] => 1 [games] => 3 ) [Team4] => Array ( [brd_points] => 7 [ttl_points] => 0 [games] => 2 ) )1 // Second array - individual rounds. Array ( [1] => Array ( [Team3] => Array ( [brd_points] => 4 [ttl_points] => 2 ) [Team2] => Array ( [brd_points] => 6 [ttl_points] => 0 ) [Team4] => Array ( [brd_points] => 6 [ttl_points] => 0 ) [Team1] => Array ( [brd_points] => 2 [ttl_points] => 2 ) [Team5] => Array ( [brd_points] => Bye [ttl_points] => Bye ) ) [2] => Array ( [Team3] => Array ( [brd_points] => 5 [ttl_points] => 2 ) [Team4] => Array ( [brd_points] => 1 [ttl_points] => 0 ) [Team5] => Array ( [brd_points] => 7 [ttl_points] => 2 ) [Team2] => Array ( [brd_points] => 5 [ttl_points] => 0 ) [Team1] => Array ( [brd_points] => Bye [ttl_points] => Bye ) ) [3] => Array ( [Team2] => Array ( [brd_points] => 5 [ttl_points] => 1 ) [Team3] => Array ( [brd_points] => 5 [ttl_points] => 1 ) [Team5] => Array ( [brd_points] => 5 [ttl_points] => 1 ) [Team1] => Array ( [brd_points] => 5 [ttl_points] => 1 ) [Team4] => Array ( [brd_points] => Bye [ttl_points] => Bye ) ) ) 1 Again thanks for your help!! Quote Link to comment https://forums.phpfreaks.com/topic/274209-sorting-subarrays-by-another-array-keys/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 8, 2013 Share Posted February 8, 2013 Is the data you have been posting in your threads coming from a database query? If so, you can generally do all this ordering/combining/processing in the query statement and just display the result from the query. Quote Link to comment https://forums.phpfreaks.com/topic/274209-sorting-subarrays-by-another-array-keys/#findComment-1411005 Share on other sites More sharing options...
Zugzwangle Posted February 8, 2013 Author Share Posted February 8, 2013 No, it is not directly from a query. I have already had to play around with the data to get the format above.. I think usort is the way to go, but I am trying to work out how to use usort!! Thanks for replying anyway. Zugz Quote Link to comment https://forums.phpfreaks.com/topic/274209-sorting-subarrays-by-another-array-keys/#findComment-1411007 Share on other sites More sharing options...
Zugzwangle Posted February 8, 2013 Author Share Posted February 8, 2013 I found my answer!! function sort_array($array, $orderArray) { $ordered = array(); foreach($orderArray as $key => $value) { if(array_key_exists($key,$array)) { $ordered[$key] = $array[$key]; unset($array[$key]); } } return $ordered + $array; } Thanks for reading anyhow. Quote Link to comment https://forums.phpfreaks.com/topic/274209-sorting-subarrays-by-another-array-keys/#findComment-1411055 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.