Home  >  Article  >  Backend Development  >  How to count the number of specific strings in an array in php? Two ways to share

How to count the number of specific strings in an array in php? Two ways to share

PHPz
PHPzOriginal
2023-03-22 11:24:412293browse

In PHP programming, we often need to find how many specific strings there are in an array. This article will explain how to use PHP built-in functions to find the number of a specific string in an array.

In PHP, there are many ways to find a string in an array. We can use a loop to iterate through the array and count, or we can use PHP built-in functions to achieve this. The two methods are introduced below.

Method 1: Use a loop to traverse the array and count

This method is relatively basic. We need to use a foreach loop to traverse the array and use a counter to record specific The number of strings. The code is as follows:

<?php
$array = array(&#39;Hello&#39;, &#39;World&#39;, &#39;PHP&#39;, &#39;Hello&#39;);
$count = 0;
foreach ($array as $value) {
    if ($value == &#39;Hello&#39;) {
        $count++;
    }
}
echo "The number of Hello in the array is :\n ".$count;
?>

Explain this code:

  • First, we define an array $array, which contains four elements: 'Hello', ' World', 'PHP' and 'Hello'.

  • Next, we define a counter $count, whose initial value is 0.

  • Then, we use a foreach loop to loop through the array $array and assign each element of the array to $value.

    In each loop, we use the if statement to check whether $value is equal to 'Hello', and if so, increase the value of the counter $count by 1.

  • Finally, we output the result, which is the number of specific strings in the array.

Method 2: Use PHP’s built-in function array_count_values()

PHP provides a built-in function array_count_values() that can quickly count arrays The number of occurrences of each element. The code is as follows:

<?php
$array = array(&#39;Hello&#39;, &#39;World&#39;, &#39;PHP&#39;, &#39;Hello&#39;);
$result = array_count_values($array);
$count = $result[&#39;Hello&#39;];
echo "The number of Hello in the array is :\n ".$count;
?>

Explain this code:

  • First, we define an array $array, which contains four elements: 'Hello', ' World', 'PHP' and 'Hello'.

  • Next, we use the PHP built-in function array_count_values() to count the number of occurrences of each element in the array $array and assign the result to $result.

  • Then, we get the number of occurrences of a specific string from $result, which is $result['Hello'].

  • Finally, we output the result, which is the number of specific strings in the array.

Summary

This article describes two methods of finding the number of specific strings in an array: using a loop to loop through the array and counting and using the PHP built-in Function array_count_values(). Among them, the method of using built-in functions is relatively simple, but may take up more memory. In actual programming, you can choose the appropriate method according to the specific situation.

The above is the detailed content of How to count the number of specific strings in an array in php? Two ways to share. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn