cristyen Posted April 3, 2013 Share Posted April 3, 2013 HI everybody,I have this multidimensional array ($TableauNotes[$k][$j]), see image 1. and i wanted to display duplicate values of column 6 in a new array like this (see image 2) with this function: function array_count_values_of($value, $TableauNotes) { $counts = array_count_values($TableauNotes); return $counts[$value]; } Can you help me how to do that? Thank you very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/276454-display-duplicate-values-in-multidimensional-array/ Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 PHPFreaks.com Questions, Comments, & Suggestions This is NOT a help forum! Do not post topics asking for help not related to the website. -- Are you getting this array from a database? Quote Link to comment https://forums.phpfreaks.com/topic/276454-display-duplicate-values-in-multidimensional-array/#findComment-1422563 Share on other sites More sharing options...
cristyen Posted April 3, 2013 Author Share Posted April 3, 2013 -- Are you getting this array from a database? I'm sorry i thought it was a help forum. This is a gradebook. The points are in a database (mysql) but the grades are calculating according to a formula. Quote Link to comment https://forums.phpfreaks.com/topic/276454-display-duplicate-values-in-multidimensional-array/#findComment-1422591 Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 How about posting some more code? Like the stuff that queries the database, loads the numbers into the array, and outputs it all. Quote Link to comment https://forums.phpfreaks.com/topic/276454-display-duplicate-values-in-multidimensional-array/#findComment-1422599 Share on other sites More sharing options...
cristyen Posted April 3, 2013 Author Share Posted April 3, 2013 (edited) How about posting some more code? Like the stuff that queries the database, loads the numbers into the array, and outputs it all. Thanks for your response. Here is the code to query the DB $query = 'SELECT (@ROW := @ROW + 1) AS NoLigne, nom, prenom, gr, sex, dob, PT1, PT2, PT3, PT4, PT5, PT6, PT7, PT8, CN1, CN2, CN3, CN4, CN5, CN6, CN7, CN8, PX1, PX2, PX3, PX4, PX5, PX6, PX7, PX8 FROM elevespoints'.$semestre.' JOIN elevespointsdesc'.$semestre.' ON elevespointsdesc'.$semestre.'.NoCours = elevespoints'.$semestre.'.NoCours JOIN (SELECT @ROW := 0) r WHERE elevespoints'.$semestre.'.NoCours='.$NoCours.' ORDER BY nom'; $rs = mysql_query($query); And here is the code to calculate the grades: $Somme = 0; $Total = 0; for ($j=1;$j<9;$j++) { $NbPt = $row ['PT'.$j]; $PMax = $row ['PX'.$j]; if (floatval($NbPt) != 0 and floatval($PMax) != 0) { $Note = number_format((float)round((floatval($NbPt) / floatval($PMax) * 5) + 1,1), 1, '.', ''); $Note = round($Note * 2) / 2; // on compte la colonne dans le tableau du nombre de notes verticales if (!isset($NbNotesV [$j])) { $NbNotesV [$j] = 0; } $NbNotesV [$j] ++; // on rempli le tableau de toutes les notes (ligne, colonne) $TableauNotes[$i][$j] = $Note; } else $Note = ''; } Edited April 3, 2013 by cristyen Quote Link to comment https://forums.phpfreaks.com/topic/276454-display-duplicate-values-in-multidimensional-array/#findComment-1422608 Share on other sites More sharing options...
cristyen Posted April 3, 2013 Author Share Posted April 3, 2013 (edited) In fact to call the function on post #1 i use this: $TableauNotes = array($TableauNotes[1][6], $TableauNotes[2][6], $TableauNotes[3][6], $TableauNotes[4][6], $TableauNotes[5][6], $TableauNotes[6][6], $TableauNotes[7][6], $TableauNotes[8][6], $TableauNotes[9][6], $TableauNotes[10][6], $TableauNotes[11][6], $TableauNotes[12][6], $TableauNotes[13][6], $TableauNotes[14][6], $TableauNotes[15][6], $TableauNotes[16][6], $TableauNotes[17][6]); echo count(array_filter($TableauNotes, function ($n) { return $n == 4.5; })); But i don't like it, i want to display the result like the image 2 on post #1 Any idea? Edited April 3, 2013 by cristyen Quote Link to comment https://forums.phpfreaks.com/topic/276454-display-duplicate-values-in-multidimensional-array/#findComment-1422611 Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 (edited) array_count_values() should work at that point, except it doesn't work on non-string, non-integer array items (because they end up as array keys). So you have two options: 1. Multiply everything by some constant so that everything is an integer (like 2 or 10), then count. $TableauNotesCounts = array_count_values(array_map(function($a) { return $a * 2; }, $TableauNotes)); 2. Since 6 is apparently the magic number, do this work in that code you had earlier that calculated the totals. At its simplest, keep an array of counts $TableauNotesCounts = array(); and add to it when you calculate something for $j=6. It's trickier because you can't just use 6.0 or 4.5 as array keys... if ($j == 6) { if (isset($TableauNotesCounts[$Note * 2])) { $TableauNotesCounts[$Note * 2]++; } else { $TableauNotesCounts[$Note * 2] = 1; } } In both cases, when you loop over the array of counts you'll need to divide the key by the constant (eg, 2) to get the original score. Edited April 3, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/276454-display-duplicate-values-in-multidimensional-array/#findComment-1422701 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.