Given Database Table
Expected Output
- No of Occurrence of A
- No of Occurrence of B
- No of Occurrence of C
- ...
using PHP
Given Database Table
Expected Output
using PHP
Actually this is something you would solve much more easily using SQL, not PHP:
SELECT
Agent,
COUNT(Agent)
FROM
yourtable
GROUP BY
Agent
Results:
Agent COUNT(Agent)
--------------------
A 4
B 4
C 2
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=86482e0d543a21ed7b0a22d7ab498e7e
This is called an "aggregate query", where you group the results by a specific column and the form a function on those results.
More info about COUNT and GROUP BY can be found in the MySQL documentation, and numerous SQL tutorials etc around the web.