I have a form that asks for up to 20 1-4 character codes. What I'm trying to do is get a count of each code, so that I end up with an output like
AB //entered once
EFOO //entered once
AACC x 2 //because AACC was entered twice into form
DDFE x 6 //because DDFE was entered into the form 6 times
Here is an example of what my code looks like, I get all the results, I'm just not sure how to compare and count them.
<input type="text" name="code[]">
foreach ($_POST as $key=>$val) {
echo $_POST['code'][$key];
}
Although @PaulProgrammer's answer is correct, php has got a built-in function for that: array_count_values()
So in your case you can just do:
$frequencies = array_count_values($_POST);
Hash table.
foreach ($_POST as $key=>$val) {
$counter[$val] ++;
}
now $counter
has keys that are the input (i.e. AACC), and value of how many times they appeared.