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..
You can use
explodefunction to make an array from the string and then usearray_count_valueswhich returns an array with the count of all the values.Note:
array_count_valuesis 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 usingstrtoupperorstrtolower.