I tried this
$data = "air;air;air;bus;air;air;bus;air;air";
$a = substr_count($data,"air");
$b = substr_count($data,"bus");
$data = implode($a . ' x ', array_unique(explode('air;', $data)));
echo $data;
And I get
7 x bus;7 x air
What I actually want to get is
7 x air 2 x bus
Help please..
2
Answers
You can use
explode
function to make an array from the string and then usearray_count_values
which returns an array with the count of all the values.Note:
array_count_values
is case-sensitive and if you want to count the values with case-insensitive, first you need to make the whole string upper case or lower case usingstrtoupper
orstrtolower
.Your code worked! However you did something weird to display it.
If we use the simplest way to show it you can see it works:
However this can be more dynamic: