Jump to content

display duplicate values in multidimensional array


cristyen

Recommended Posts

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.

 

post-145732-0-84963400-1364953242_thumb.png

post-145732-0-76509000-1364953270_thumb.png

Link to comment
Share on other sites

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 by cristyen
Link to comment
Share on other sites

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 by cristyen
Link to comment
Share on other sites

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 by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.