galvin Posted September 6, 2012 Share Posted September 6, 2012 I have an array called "fullrosterafter" and it contains the info you see at the very bottom of this post. The first index is positionid (1 thru 4 where 1 is QB, 2 is RB, 3 is WR and 4 is TE). Then the subarrays (which holds all the info) have an index of playerid. I simply want to sort this entire array by "positionid" first and then by "ranking" (It's basically in order by positionid already, but assume it's not). When I run the code below (it's one of about 15 attempts I've made and it's wrong, but figured I'd have to show you something , it's not sorting by "ranking" properly. What am I doing wrong? foreach ($fullrosterafter as $ID => $row) { $positionid[$ID] = $row['positionid']; $ranking[$ID]= $row['ranking']; } array_multisort($positionid, SORT_ASC, $ranking, SORT_ASC, $fullrosterafter); Array ( [1] => Array ( [2] => Array ( [playerid] => 2 [ranking] => 3 [positionid] => 1 [lastname] => Brees [firstname] => Drew [teamid] => 10 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => qb [nickname] => Saints [location] => New Orleans [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => [involved] => involved ) [37] => Array ( [playerid] => 37 [ranking] => 18 [positionid] => 1 [lastname] => Freeman [firstname] => Josh [teamid] => 9 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => qb [nickname] => Buccaneers [location] => Tampa Bay [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => [involved] => involved ) [450] => Array ( [playerid] => 450 [ranking] => 26 [positionid] => 1 [lastname] => Wilson [firstname] => Russell [teamid] => 13 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => qb [nickname] => Seahawks [location] => Seattle [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => [involved] => involved ) ) [2] => Array ( [74] => Array ( [playerid] => 74 [ranking] => 4 [positionid] => 2 [lastname] => Johnson [firstname] => Chris [teamid] => 26 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => rb [nickname] => Titans [location] => Tennessee [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => ) [108] => Array ( [playerid] => 108 [ranking] => 1 [positionid] => 2 [lastname] => Foster [firstname] => Arian [teamid] => 25 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => rb [nickname] => Texans [location] => Houston [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => [involved] => involved ) [80] => Array ( [playerid] => 80 [ranking] => 24 [positionid] => 2 [lastname] => McGahee [firstname] => Willis [teamid] => 29 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => rb [nickname] => Broncos [location] => Denver [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => [involved] => involved ) ) [3] => Array ( [258] => Array ( [playerid] => 258 [ranking] => 2 [positionid] => 3 [lastname] => Jones [firstname] => Julio [teamid] => 12 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => wr [nickname] => Falcons [location] => Atlanta [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => ) [211] => Array ( [playerid] => 211 [ranking] => 5 [positionid] => 3 [lastname] => Nicks [firstname] => Hakeem [teamid] => 3 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => wr [nickname] => Giants [location] => New York [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => ) [267] => Array ( [playerid] => 267 [ranking] => 33 [positionid] => 3 [lastname] => Moore [firstname] => Denarius [teamid] => 31 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => wr [nickname] => Raiders [location] => Oakland [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => ) [417] => Array ( [playerid] => 417 [ranking] => 125 [positionid] => 3 [lastname] => Toon [firstname] => Nick [teamid] => 10 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => wr [nickname] => Saints [location] => New Orleans [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => [involved] => involved ) ) [4] => Array ( [298] => Array ( [playerid] => 298 [ranking] => 6 [positionid] => 4 [lastname] => Finley [firstname] => Jermichael [teamid] => 7 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => te [nickname] => Packers [location] => Green Bay [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => ) [311] => Array ( [playerid] => 311 [ranking] => 16 [positionid] => 4 [lastname] => Cook [firstname] => Jared [teamid] => 26 [feet] => 0 [inches] => 0 [weight] => 0 [born] => 0000-00-00 [college] => [display] => YES [ppradjustment] => 0 [injured] => no [sportid] => 1 [position] => te [nickname] => Titans [location] => Tennessee [qb_matchup] => [rb_matchup] => [wr_matchup] => [te_matchup] => ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/268048-array_multisort-problems/ Share on other sites More sharing options...
Psycho Posted September 6, 2012 Share Posted September 6, 2012 Didn't we go through this already in one of your other posts? You should do the sorting using your query rather than processing the data multiple times. $sql = "SELECT * FROM players ORDER BY positionid, ranking"; Also, if you are not going to use ALL of those fields in the output do not use '*' in your SELECT and instead list out the fields you will use. Quote Link to comment https://forums.phpfreaks.com/topic/268048-array_multisort-problems/#findComment-1375648 Share on other sites More sharing options...
galvin Posted September 6, 2012 Author Share Posted September 6, 2012 Yes, we did discuss that option before, but as I said then, it's not feasible to do it in the query (in this case) because there isn't one single query bringing back the data that ends up in the array I'm trying to sort. The array is built in multiple steps from multiple queries so the sorting needs to be done after it's fully built. Quote Link to comment https://forums.phpfreaks.com/topic/268048-array_multisort-problems/#findComment-1375649 Share on other sites More sharing options...
Psycho Posted September 6, 2012 Share Posted September 6, 2012 Yes, we did discuss that option before, but as I said then, it's not feasible to do it in the query (in this case) because there isn't one single query bringing back the data that ends up in the array I'm trying to sort. The array is built in multiple steps from multiple queries so the sorting needs to be done after it's fully built. Then you ushould do ONE query using appropriate JOINs, but I think you were given that advice previously and ignored it as well. So, if you are going to plow ahead with this method in the face of multiple assertions as to why it is a terrible idea, then I would say you should use usort() instead of array_multisort() it is much easier and much more flexible IMHO: //Custom function to use with usort() function sortPlayers($a, $b) { //Position ID is not the same, sort on that value if($a['positionid'] != $b['positionid']) { return $a['positionid'] - $b['positionid']; } //Ranking is not the same, sort on that value if($a['ranking'] != $b['ranking']) { return $a['ranking'] - $b['ranking']; } //PositionID and ranking are the same - no sort pref return 0; } usort($fullrosterafter, 'sortPlayers'); You may need to reverse the $a['index'] - $b['index'] to get the order you want them sorted in. Quote Link to comment https://forums.phpfreaks.com/topic/268048-array_multisort-problems/#findComment-1375651 Share on other sites More sharing options...
galvin Posted September 6, 2012 Author Share Posted September 6, 2012 Your assumptive/condescending nature is a tad off-putting , specifically how you are so sure that I can use ONE mysql query with joins when you have no idea about the overall scope of what processes are taking place as users are doing things on my site. With that said, I 100% appreciate you taking time to respond and give advice, and I totally respect that you are way more knowledgeable about this stuff, so I will go back over everything again with a fine tooth comb and see if there is some way that I can get all the data for every player up-front using one query (and sort it properly) and then pull from that huge array for the rest of my processes. It just seems on the surface that pulling all the data for 500+ players just so I can sort it, even though I will only end up using about 10-15 players (ased on what users select throughout the course of my site's processes) is also a bad idea. But maybe that is the better way? In other words, is it always better to do ONE query and make a large array of data which I'll use multiple times later on, as opposed to doing a handful of much smaller queries throughout the page's code? I know you probably can't answer that definitively (i.e. it's probably a loaded question dependant on many variables), but I'm just thinking out loud. No need to reply. Anyway, again, I am an ignorant newbie (totally aware of that) so I will go back to the drawing board and see if I can make all my processes work by doing just ONE query at the beginning and working off of a large array. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/268048-array_multisort-problems/#findComment-1375726 Share on other sites More sharing options...
Psycho Posted September 6, 2012 Share Posted September 6, 2012 My apologies if I offended you, but I believe the whole reason you are having to post so many topics is the fact that you are chasing the wrong solution. Several people have tried to provide the same guidance, yet it seems to fall on deaf ears. It just seems on the surface that pulling all the data for 500+ players just so I can sort it, even though I will only end up using about 10-15 players (ased on what users select throughout the course of my site's processes) is also a bad idea. That is the problem! You are making decisions based upon your preconceived assumptions and not based upon actual understanding or experience. Just because you are JOINing many tables with a complicated structure does not mean you can't also limit and order the results as you need them. This is infinitely more efficient than running multiple queries. It sounds like you really are making this more difficult than it needs to be. What I see you doing is making the same mistakes I see a lot of people using when they first start. We're trying to help you by learning the right way to do it instead of ingraining bad habits. But, I will state that learning to really use a database is not a trivial task. So, I understand your inclination to continue with what makes sense to you rather than trying to learn some new skills that might be a little intimidating. When you post a help request you have already decided upon a specific process which may be flawed. But, your request is so specific to the point in the process that you are stuck on we can only reply along the lines of "you're doing it wrong" and perhaps throw you a bone about using a single query. On the other hand, if you were to provide more information about the data structure you have and what you are trying to get from that data we could, perhaps, provide a much better solution. . . . is it always better to do ONE query and make a large array of data which I'll use multiple times later on, as opposed to doing a handful of much smaller queries throughout the page's code? I know you probably can't answer that definitively . . . Again, the concept is flawed. You shouldn't need to do one big query and store the data into an array to use later. You query for the data you need when you need it. In fact, in most cases you shouldn't even need to store DB results in an array. You should only need to process the data ONE TIME when you extract the records from the result. But, that should not be taken to the extreme. If you were going to display different tables of data for each position, for example, you would still probably do only one query and then create the multiple tables as you process the data. So, if you want some real help, let's start over. If you can provide 1) The table structures that you have, 2) Explain what data you are wanting to display, and 3) The code you are currently using to get the data - then I will try to provide a query/process to meet your needs. Quote Link to comment https://forums.phpfreaks.com/topic/268048-array_multisort-problems/#findComment-1375747 Share on other sites More sharing options...
galvin Posted September 6, 2012 Author Share Posted September 6, 2012 You didn't really offend me, I think you just sternly told me (and rightfully so) that I am going about things all wrong and I guess I felt a little like a student being scolded by a teacher, but that essentially is our relationship haha (since you clearly know your stuff). My bad. I can now appreciate you getting frustrated with someone for doing things the wrong way because it shows you care and want to help. And you're absolutely right when you say I decide on one way to do something and ask for help on doing it that way even if it's wrong. I just can't seem to grasp normalizing databases, properly querying them and properly using arrays (you know, basically all the core functions of programming haha). So I often figure out how to get something to work and am just so happy it works, I don't think about if I did it the most efficient way. I will try to write up my general plan the best I can and post it to you. I am NOT asking you to write any code for me, but if my writeup makes sense and you can provide some general direction on properly handling it, then maybe I could at least try to move a little closer towards doing things the proper way. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/268048-array_multisort-problems/#findComment-1375751 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.