Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This data simulates your database table
- $rowOne = array(1, 3, 5, 2, 6);
- $rowTwo = array(4, 7, 8, 9, 8);
- $rowThree = array(2, 6, 9, 1, 0);
- $rowFour = array(3, 4, 8, 8, 3);
- // This simulates a query for all the rows and columns in your db
- $allRows = array($rowOne, $rowTwo, $rowThree, $rowFour);
- // these are the numbers you want to count...notice there are no duplicates no need
- $numberCounts = array(3 => 0, 4 => 0, 8 => 0);
- // go through each row...
- foreach ($allRows as $row) {
- // go through each value in the row...
- foreach ($row as $value) {
- // compare it to each number we want to count
- foreach ($numberCounts as $numberToCount => $occurences) {
- if ($numberToCount === $value) {
- $numberCounts[$numberToCount]++;
- }
- }
- }
- }
- var_dump($numberCounts);
- // with sample data will print out
- // array (
- // 3 => 3,
- // 4 => 2,
- // 8 => 4
- // )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement