0

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..

9
  • You say you wanted to count words, so why are you actually counting single letters instead? Commented Sep 13, 2023 at 13:13
  • 1
    echo implode($a . ' x ', array_unique(explode('air;', $data))); - that makes so little sense, I would not even know where to start ... Just explode your string at the ;, and then use php.net/manual/en/function.array-count-values.php Commented Sep 13, 2023 at 13:15
  • @CBroe - sorry i made typo mistake. I have revised it into counting word. Commented Sep 13, 2023 at 13:15
  • 1
    So what is with that overly complicated last line, why don't you simply do echo $a . ' x air ' . $b . ' x bus'; ...? Commented Sep 13, 2023 at 13:17
  • @CBroe - Because I want to replace $data - (I just revised my code again to better reflex it. Sorry) Commented Sep 13, 2023 at 13:26

2 Answers 2

2

You can use explode function to make an array from the string and then use array_count_values which returns an array with the count of all the values.

<?php
$data = "air;air;air;bus;air;air;air;air";

$arrayData = explode(';', $data);
$counts = array_count_values($arrayData);

$parsedData = '';
foreach ($counts as $key => $val) {
    $parsedData .= $val > 1 ? "$val x $key, " : "$key, ";
}

echo rtrim($parsedData, ', ');

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 using strtoupper orstrtolower.

Sign up to request clarification or add additional context in comments.

7 Comments

I want to replace $data with 7 x air 2 x bus - (I just revised my code in question again to better reflex it. Sorry)
@Pekzz I changed my code. I like to create a new variable because "Explicit is better than implicit", but you can replace parsedData with data wherever it's used.
@Pekzz the values can be only air and bus? If there's no bus, still you want to show it in the result?
Thank you. This works well. However, sometimes my initial $data only contains air or bus or nothing. How to display only air or bus or nothing without showing 1 x ?
@Pekzz, You're welcome. I updated the code and added array_filter to filter out all empty values from the array. If we give an empty string to explode, it will return an array containing an element, which is an empty string.
|
1

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:

<?php
$data = "air;air;air;bus;air;air;bus;air;air";

$a = substr_count($data,"air");
$b = substr_count($data,"bus");

echo $a . ' x ' . 'air' . ' ' . $b . ' x ' . 'bus';

However this can be more dynamic:

<?php
$data = "air;air;air;bus;air;air;bus;air;air";

$words = array_unique(explode(';', $data));

foreach ($words as $word) {
    echo substr_count($data, $word) . ' x ' .$word;
    echo ' ';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.