galvin Posted August 16, 2012 Share Posted August 16, 2012 I swear I have just been reading through ksort and asort on php.net for 45 minutes and I can't figure out what I need to do. I have an array that had data entered in this format... $fullrosterafter[$row['playerid']][ranking]=$row['ranking']; $fullrosterafter[$row['playerid']][positionid]=$row['positionid']; $fullrosterafter[$row['playerid']][position]=$row['position']; $fullrosterafter[$row['playerid']][teamid]=$row['teamid']; $fullrosterafter[$row['playerid']][nickname]=$row['nickname']; $fullrosterafter[$row['playerid']][location]=$row['location']; $fullrosterafter[$row['playerid']][firstname]=$row['firstname']; $fullrosterafter[$row['playerid']][lastname]=$row['lastname']; So once it had data, an example of how the print_r would look is... Array ( [18] => Array ( [positionid] => 1 [ranking] => 7 [position] => qb [teamid] => 29 [nickname] => Broncos [location] => Denver [firstname] => Peyton [lastname] => Manning ) [74] => Array ( [positionid] => 2 [ranking] => 6 [position] => rb [teamid] => 26 [nickname] => Titans [location] => Tennessee [firstname] => Chris [lastname] => Johnson ) [133] => Array ( [positionid] => 2 [ranking] => 15 [position] => rb [teamid] => 2 [nickname] => Cowboys [location] => Dallas [firstname] => DeMarco [lastname] => Murray ) [208] => Array ( [positionid] => 3 [ranking] => 18 [position] => wr [teamid] => 6 [nickname] => Vikings [location] => Minnesota [firstname] => Percy [lastname] => Harvin ) [258] => Array ( [positionid] => 3 [ranking] => 2 [position] => wr [teamid] => 12 [nickname] => Falcons [location] => Atlanta [firstname] => Julio [lastname] => Jones ) [211] => Array ( [positionid] => 3 [ranking] => 14 [position] => wr [teamid] => 3 [nickname] => Giants [location] => New York [firstname] => Hakeem [lastname] => Nicks ) [298] => Array ( [positionid] => 4 [ranking] => 4 [position] => te [teamid] => 7 [nickname] => Packers [location] => Green Bay [firstname] => Jermichael [lastname] => Finley ) [6] => Array ( [positionid] => 1 [ranking] => 16 [position] => qb [teamid] => 8 [nickname] => Bears [location] => Chicago [firstname] => Jay [lastname] => Cutler ) [88] => Array ( [positionid] => 2 [ranking] => 9 [position] => rb [teamid] => 10 [nickname] => Saints [location] => New Orleans [firstname] => Darren [lastname] => Sproles ) [267] => Array ( [positionid] => 3 [ranking] => 26 [position] => wr [teamid] => 31 [nickname] => Raiders [location] => Oakland [firstname] => Denarius [lastname] => Moore ) [311] => Array ( [positionid] => 4 [ranking] => 15 [position] => te [teamid] => 26 [nickname] => Titans [location] => Tennessee [firstname] => Jared [lastname] => Cook ) ) All I want to do is cycle through this array and sort the output by "positionid" first (1, then 2, then 3, up through 6) and within each group of positions, I want to sort by "ranking". So QBs (positionid of 1) would come first. And Peyton Manning would be the very first player outputted since his ranking is 7. Jay Cutler would be next with a ranking of 16. Then it would move on to the RBs (positionid of 2). And so on. I tried all sorts of things in the ksort function, but nothing seems to change the order of my output. ksort(?????); //cannot figure out what to put here, which makes me think maybe I shouldn't be using ksort at all foreach ($fullrosterafter as $ID => $info) { $output .="<div class='$class'>" . strtoupper($info['position']) . " - " . $info['firstname'] . " " . $info['lastname'] . ", " . $info['nickname'] . " (" . $info['ranking'] . ")</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/267154-trouble-sorting-an-array-by-specific-value/ Share on other sites More sharing options...
MMDE Posted August 16, 2012 Share Posted August 16, 2012 Obviously, they sort arrays. ksort example: $array = array('d'=>'one', 'c'=>'two', 'a'=>'three', 'b'=>'four'); print_r($array); foreach($array AS $key=>$val){ echo $key.'=>'.$value."<br/>\n"; } ksort($array); print_r($array); foreach($array AS $key=>$val){ echo $key.'=>'.$value."<br/>\n"; } try to run that, and you will probably understand ksort. asort example: $array = array('d'=>'one', 'c'=>'two', 'a'=>'three', 'b'=>'four'); print_r($array); foreach($array AS $key=>$val){ echo $key.'=>'.$value."<br/>\n"; } asort($array); print_r($array); foreach($array AS $key=>$val){ echo $key.'=>'.$value."<br/>\n"; } try to run that, and you will probably understand asort. Quote Link to comment https://forums.phpfreaks.com/topic/267154-trouble-sorting-an-array-by-specific-value/#findComment-1369777 Share on other sites More sharing options...
maxudaskin Posted August 16, 2012 Share Posted August 16, 2012 http://www.php.net/manual/en/function.array-multisort.php#example-4608 Quote Link to comment https://forums.phpfreaks.com/topic/267154-trouble-sorting-an-array-by-specific-value/#findComment-1369778 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2012 Share Posted August 16, 2012 Or you could sort the data in your query - ORDER BY positionid, ranking Quote Link to comment https://forums.phpfreaks.com/topic/267154-trouble-sorting-an-array-by-specific-value/#findComment-1369781 Share on other sites More sharing options...
galvin Posted August 16, 2012 Author Share Posted August 16, 2012 I wish I could sort it in the query, but I'm building the array from multiple queries, so it's not feasible. So I'm trying to use array_multisort, per maxudaskin's suggestion. But when I do this below, it gives an error of "Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in..." foreach ($fullrosterafter as $ID => $info) { // Obtain a list of columns $positionid[$ID] = $info['positionid']; $ranking[$ID] = $info['ranking']; array_multisort($positionid, SORT_ASC, $ranking, SORT_ASC, $fullrosterafter); print_r($array); } FYI, the array is created with these elements... $fullrosterafter[$row['playerid']][ranking]=$row['ranking']; $fullrosterafter[$row['playerid']][positionid]=$row['positionid']; $fullrosterafter[$row['playerid']][position]=$row['position']; $fullrosterafter[$row['playerid']][teamid]=$row['teamid']; $fullrosterafter[$row['playerid']][nickname]=$row['nickname']; $fullrosterafter[$row['playerid']][location]=$row['location']; $fullrosterafter[$row['playerid']][firstname]=$row['firstname']; $fullrosterafter[$row['playerid']][lastname]=$row['lastname']; and the final array (which I later try to loop through with the array_multisort code above) looks like this... Array ( [133] => Array ( [positionid] => 2 [ranking] => 15 [position] => rb [teamid] => 2 [nickname] => Cowboys [location] => Dallas [firstname] => DeMarco [lastname] => Murray ) [208] => Array ( [positionid] => 3 [ranking] => 18 [position] => wr [teamid] => 6 [nickname] => Vikings [location] => Minnesota [firstname] => Percy [lastname] => Harvin ) [298] => Array ( [positionid] => 4 [ranking] => 4 [position] => te [teamid] => 7 [nickname] => Packers [location] => Green Bay [firstname] => Jermichael [lastname] => Finley ) [88] => Array ( [positionid] => 2 [ranking] => 9 [position] => rb [teamid] => 10 [nickname] => Saints [location] => New Orleans [firstname] => Darren [lastname] => Sproles ) [311] => Array ( [positionid] => 4 [ranking] => 15 [position] => te [teamid] => 26 [nickname] => Titans [location] => Tennessee [firstname] => Jared [lastname] => Cook ) [1] => Array ( [ranking] => 3 [positionid] => 1 [position] => qb [teamid] => 20 [nickname] => Patriots [location] => New England [firstname] => Tom [lastname] => Brady ) [7] => Array ( [ranking] => 24 [positionid] => 1 [position] => qb [teamid] => 19 [nickname] => Bills [location] => Buffalo [firstname] => Ryan [lastname] => Fitzpatrick ) [4] => Array ( [ranking] => 26 [positionid] => 1 [position] => qb [teamid] => 30 [nickname] => Chiefs [location] => Kansas City [firstname] => Matt [lastname] => Cassel ) [59] => Array ( [ranking] => 21 [positionid] => 2 [position] => rb [teamid] => 18 [nickname] => Dolphins [location] => Miami [firstname] => Reggie [lastname] => Bush ) [104] => Array ( [ranking] => 28 [positionid] => 2 [position] => rb [teamid] => 28 [nickname] => Colts [location] => Indianapolis [firstname] => Donald [lastname] => Brown ) [464] => Array ( [ranking] => 104 [positionid] => 2 [position] => rb [teamid] => 29 [nickname] => Broncos [location] => Denver [firstname] => Lance [lastname] => Ball ) [175] => Array ( [ranking] => 1 [positionid] => 3 [position] => wr [teamid] => 5 [nickname] => Lions [location] => Detroit [firstname] => Calvin [lastname] => Johnson ) [384] => Array ( [ranking] => 46 [positionid] => 3 [position] => wr [teamid] => 27 [nickname] => Jaguars [location] => Jacksonville [firstname] => Justin [lastname] => Blackmon ) [243] => Array ( [ranking] => 67 [positionid] => 3 [position] => wr [teamid] => 4 [nickname] => Redskins [location] => Washington [firstname] => Anthony [lastname] => Armstrong ) [279] => Array ( [ranking] => 13 [positionid] => 4 [position] => te [teamid] => 32 [nickname] => Chargers [location] => San Diego [firstname] => Antonio [lastname] => Gates ) ) 1. Any idea why I'm getting this inconsistent error? 2. I googled and someone said you need commas in the array. But I added commas, but it didn't help. However, I'm still curious if I should be using commas. So a side question: Should I get in the habit of putting commas in my arrays? Quote Link to comment https://forums.phpfreaks.com/topic/267154-trouble-sorting-an-array-by-specific-value/#findComment-1369880 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2012 Share Posted August 16, 2012 You should be using a single joined query to get all the related data you want in the order that you want it with one query. If you output a html <pre> tag before you use print_r on your array, the output will be formated so that it is readable. If you reread the multisort examples, you will see that the array_multisort statement is after the foreach(){} loop that is extracting the values to sort on. Quote Link to comment https://forums.phpfreaks.com/topic/267154-trouble-sorting-an-array-by-specific-value/#findComment-1369884 Share on other sites More sharing options...
galvin Posted August 16, 2012 Author Share Posted August 16, 2012 You should be using a single joined query to get all the related data you want in the order that you want it with one query. I really don't think I can with the nature of my data, but I will brainstorm some more to make sure. In very general plain english, I start with a roster, then some stuff happens, a mysql query is done to get info about new players and the changes are made to the full roster array (removing and adding players). Then some more stuff is done, another separate mysql query is done and more changes are made to the full roster array and then I end up with my new, changed full roster array. Even if I sort both queries by position, ranking, the ultimate array will still not be in that perfect order. After the first query it will be in perfect order, but if the second query ends with another QB being added to the array, then he gets added to the end of the array ( after other RBs, WRs, TEs, etc). If you output a html <pre> tag before you use print_r on your array, the output will be formated so that it is readable. How I'm just learning about this is maddening Thank you SO much! Very helpful. If you reread the multisort examples, you will see that the array_multisort statement is after the foreach(){} loop that is extracting the values to sort on. Ugh, stupid, stupid, stupid. Thank you for pointing that out. Quote Link to comment https://forums.phpfreaks.com/topic/267154-trouble-sorting-an-array-by-specific-value/#findComment-1369916 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.