PHP query the number of specified elements in an array

WBOY
Release: 2023-05-06 12:56:10
Original
1231 people have browsed it

In PHP, array is a very common and useful data type. When you need to calculate the number of specific elements in an array, you can use the built-in functions provided by PHP to make the operation simpler and easier to use.

This article will introduce how to use PHP built-in functions to query the number of specified elements in an array. Through the introduction of this article, readers will learn how to use PHP arrays, how to traverse arrays, how to use built-in functions to calculate array length and the number of occurrences of elements, etc.

1. Use the array_count_values() function to get the number of occurrences of an element

PHP provides a built-in function called array_count_values(), which is used to get the number of occurrences of an element in an array. This function receives an array as a parameter and returns a new array. The key of this new array is the element value in the original array, and the value is the number of times this element value appears in the original array.

The following is an example of using the array_count_values() function to calculate the number of occurrences of array elements:

$arr = array(1, 2, 3, 2, 2, 4, 5, 5);
$counts = array_count_values($arr);
print_r($counts);
Copy after login

The execution results are as follows:

Array
(
    [1] => 1
    [2] => 3
    [3] => 1
    [4] => 1
    [5] => 2
)
Copy after login

In the above example, we define an array $ arr, which has 8 elements, then the array_count_values() function is called, passing $arr as a parameter to the function, and the function returns a new array $counts, which represents the number of occurrences of each element in $arr. As you can see, 2 appears 3 times in the array $arr, so the value corresponding to 2 in the $counts array is 3.

However, the array_count_values() function can only count the number of occurrences of array elements whose values ​​are string or integer types. For other types of elements, the function will return false. When using this function, you need to pay attention to the type issue, otherwise it will cause errors.

2. Use a for loop to check the number of occurrences of elements one by one

In addition to using the array_count_values() function, we can also use a for loop to check the elements in the array one by one and count the number of occurrences.

The following is an example of using a for loop to calculate the number of occurrences of an array element:

$arr = array(1, 2, 3, 2, 2, 4, 5, 5);
$count = 0;
$target = 2;
for ($i = 0; $i < count($arr); $i++) {
    if ($arr[$i] === $target) {
        $count++;
    }
}
echo $target . "出现了" . $count . "次";
Copy after login

The execution results are as follows:

2出现了3次
Copy after login
Copy after login

In the above example, we defined an array $arr, There are 8 elements in it, and we also define a variable $target, which represents the target value we want to query. Then, we use a for loop to traverse the array and check the elements in the array one by one. If this element is equal to $target, add 1 to the value of the counter $count.

Finally, we output the number of times the target value $target appears in the array $arr.

It should be noted that when using a for loop to calculate the number of occurrences of an array element, you need to use the count() function to obtain the length of the array in order to loop through each array element.

3. Use foreach loop to traverse the array and count the number of occurrences of elements

In addition to using the for loop to check elements one by one, we can also use the foreach statement to traverse the array and count the number of occurrences of elements. Compared with a for loop, the foreach statement can traverse an array more concisely and clearly.

The following is an example of using a foreach loop to count the number of occurrences of array elements:

$arr = array(1, 2, 3, 2, 2, 4, 5, 5);
$count = 0;
$target = 2;
foreach ($arr as $value) {
    if ($value === $target) {
        $count++;
    }
}
echo $target . "出现了" . $count . "次";
Copy after login

The execution results are as follows:

2出现了3次
Copy after login
Copy after login

In the above example, we use the foreach statement to traverse the array $arr , for each array element, if the element value is equal to the target value $target, then add 1 to the value of the counter $count.

It should be noted that when using the foreach statement to traverse an array, you can use the $key=>$value syntax, where $key represents the subscript of the array element and $value represents the value of the array element. If you do not need to use the $key variable, you can omit it.

Summary

In PHP, it is very convenient to query the number of specified elements in an array. There are three methods to achieve this:

1. Use the array_count_values() function to obtain the occurrence of elements. Number of times

2. Use a for loop to check the number of occurrences of elements one by one

3. Use the foreach statement to traverse the array and count the number of occurrences of elements

You need to choose the appropriate method according to the specific situation. At the same time, you need to pay attention to the type of array elements to avoid errors. I hope this article can inspire and help PHP array operations.

The above is the detailed content of PHP query the number of specified elements in an array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template