mickyvelly Posted February 15, 2011 Share Posted February 15, 2011 Hey all, i have a problem; i have 2 arrays: $haystack //contains all posible answers in the poll, it contains dates for possible meetings Array ( [0] => 12-02-2011 [1] => 13-02-2011 [2] => 14-02-2011 [3] => 15-02-2011 ) $needles //contains the answers the user clicked Array ( [0] => 12-02-2011 [1] => 13-02-2011 ) i have about 10 entries simulated and i want to count the corresponding dates into another array so: Array ( [0] => 5 [1] => 3 [2] => 1 [3] => 1 ) keep in mind that the users can give multiple answers Quote Link to comment Share on other sites More sharing options...
mickyvelly Posted February 15, 2011 Author Share Posted February 15, 2011 solved it!!! function countVotes($haystack, $countAnswers, $needles, $countFunction) { //$haystack is the array where we store all the posible answers (array keys must begin from 0) //$countAnswers is the variable were we store the total amount of votes //$needles is the array with only 1 vote (but it can contain multiple answers (beware that the answers must correspond with the $haystack)) //$countFunction is the amount of times the function has been called, so the if statement knows when to unset the session variable $p = 0; // count for while loop while($p < count($needles)) {//loop while there are new needles if(is_array($needles)) {//if the needle is an array if(in_array($needles[$p], $haystack)) {//check if the needle is in the array $_SESSION['count'][$needles[$p]]++; } } else {//if the needle is not an array if(in_array($needles, $haystack)) {//check if the needle is in the array $_SESSION['count'][$needles]++; } } $p++; } if($countFunction == ($countAnswers-1)) {//if there is no new needle anymore, copy the session data to a safe variable which we will return and delete the session data $count = $_SESSION['count']; unset($_SESSION['count']); } return $count;//return the array with the counted votes }function countVotes($haystack, $countAnswers, $needles, $countFunction) { $p = 0; // count for while loop while($p < count($needles)) {//loop while there are new needles if(is_array($needles)) {//if the needle is an array if(in_array($needles[$p], $haystack)) {//check if the needle is in the array $_SESSION['count'][$needles[$p]]++; } } else {//if the needle is not an array if(in_array($needles, $haystack)) {//check if the needle is in the array $_SESSION['count'][$needles]++; } } $p++; } if($countFunction == ($countAnswers-1)) {//if there is no new needle anymore, copy the session data to a safe variable which we will return and delete the session data $count = $_SESSION['count']; unset($_SESSION['count']); } return $count;//return the array with the counted votes } sorry guys for solving the problem myself... thanks anyways Quote Link to comment 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.