I have the following array()
$statoA = array();
It has
Italy
France
France
Italy
Spain
I know I can use array_count_values($statoA)
But how to do a foreach to have:
<ul>
<li>2 France</li>
<ll>2 Italy</li>
<li>1 Spain</li>
</ul>
I have the following array()
$statoA = array();
It has
Italy
France
France
Italy
Spain
I know I can use array_count_values($statoA)
But how to do a foreach to have:
<ul>
<li>2 France</li>
<ll>2 Italy</li>
<li>1 Spain</li>
</ul>
If you are using array_count_values(), then you will have a array where the keys are the terms from your original array, and the values are the counts for each term. You can then loop through this array to build your markup:
<ul>
<?php
foreach (array_count_values($statoA) as $key => $value) {
//$key is now the term (France, Italy, Spain)
//$value is now the frequency or counts of the term
echo "<li>{$value} {$key}</li>";
}
?>
</ul>
Documentation for array_count_values is available here: http://php.net/manual/en/function.array-count-values.php
Documentation on foreach is available here: http://php.net/manual/en/control-structures.foreach.php
echo "Name: {$user->name}.". I find that more agreeable than than dot-concatenation (again, personally). Eventually I just got in the habit of doing it regardless of the data type in the variable. If there are no variables to interpret in the string, I also go so far as to specifically use apostrophes instead of quotes. Just marks intent well.