-2

I need to count how many times caracter 1, 2 ,3 ,4, 5 is existing in this array e.g. for this i should get answer 1 - two times 2 - two times 3 - one time 4 - three times

<?php
array(8) {
  [0]=>
  array(2) {
    [0]=>
    string(1) "3"
    ["answer"]=>
    string(1) "3"
  }
  [1]=>
  array(2) {
    [0]=>
    string(1) "4"
    ["answer"]=>
    string(1) "4"
  }
  [2]=>
  array(2) {
    [0]=>
    string(1) "1"
    ["answer"]=>
    string(1) "1"
  }
  [3]=>
  array(2) {
    [0]=>
    string(1) "1"
    ["answer"]=>
    string(1) "1"
  }
  [4]=>
  array(2) {
    [0]=>
    string(1) "2"
    ["answer"]=>
    string(1) "2"
  }
  [5]=>
  array(2) {
    [0]=>
    string(1) "2"
    ["answer"]=>
    string(1) "2"
  }
  [6]=>
  array(2) {
    [0]=>
    string(1) "4"
    ["answer"]=>
    string(1) "4"
  }
  [7]=>
  array(2) {
    [0]=>
    string(1) "4"
    ["answer"]=>
    string(1) "4"
  }
}
?>
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
JohnA
  • 1,875
  • 8
  • 28
  • 34

1 Answers1

0

If this is data returned from a database, why not use the database query to do the counting for you?

SELECT answer,
       COUNT(1) AS instances
  FROM myTable
 GROUP BY answer
Mark Baker
  • 209,507
  • 32
  • 346
  • 385