Advertisement
Guest User

Untitled

a guest
Dec 14th, 2011
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1.  
  2. // This data simulates your database table
  3. $rowOne = array(1, 3, 5, 2, 6);
  4. $rowTwo = array(4, 7, 8, 9, 8);
  5. $rowThree = array(2, 6, 9, 1, 0);
  6. $rowFour = array(3, 4, 8, 8, 3);
  7.  
  8. // This simulates a query for all the rows and columns in your db
  9. $allRows = array($rowOne, $rowTwo, $rowThree, $rowFour);
  10.  
  11. // these are the numbers you want to count...notice there are no duplicates no need
  12. $numberCounts = array(3 => 0, 4 => 0, 8 => 0);
  13.  
  14. // go through each row...
  15. foreach ($allRows as $row) {
  16.  
  17. // go through each value in the row...
  18. foreach ($row as $value) {
  19.  
  20. // compare it to each number we want to count
  21. foreach ($numberCounts as $numberToCount => $occurences) {
  22. if ($numberToCount === $value) {
  23. $numberCounts[$numberToCount]++;
  24. }
  25. }
  26. }
  27. }
  28.  
  29. var_dump($numberCounts);
  30.  
  31. // with sample data will print out
  32. // array (
  33. // 3 => 3,
  34. // 4 => 2,
  35. // 8 => 4
  36. // )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement