I have form data coming in and a preg match statement. What I would like it to do is when the match is true, give me a total of true statements (got that), as well as count the like items. The array is matching true properly, example array:
Array
(
[0] => Array
(
[0] => She likes lilacs!
[1] => She likes lilacs!
[2] => She likes carnations!
[3] => She likes lilacs!
[4] => She likes roses!
[5] => She likes carnations!
[6] => She likes daisies!
[7] => She likes daisies!
[8] => She likes lilacs!
[9] => She likes lillies!
[10] => She likes lillies!
)
[1] => Array
(
[0] => lilacs
[1] => lilacs
[2] => carnations
[3] => lilacs
[4] => roses
[5] => carnations
[6] => daisies
[7] => daisies
[8] => lilacs
[9] => lillies
[10] => lillies
)
It tells me I have 11 matches (as it should).
The part I'm failing is how to tell me how many of each. If I manually code such as this:
preg_match_all("/She likes (.+?)!/", $data, $flower);
$tFlower = (count($flower[1]) - 1);
$i = 0;
$newdata = array();
while($i <= $tFlower)
{
if ($flower[1][$i] == "violets")
{$violets = 'violets';
$violetsNum = $violetsNum + 1;
}
$i++;}
It will tell me that Violets equals 2 when I echo the 2 variables. What I would like to do is have it create the variable violets and add 1 the first time it is come upon, and each time violets come up again, add 1 again. Doing the same for any other flower type.
Bottom line is, I'd like to automate the process, and at the end have:
Violets: 2
Lilacs: 6
Roses: 1
Daises: 2
Total ($i): 11
How do I automate this process so I do not have to know each "flower", and the script can determine if there have been others before?
tia!