Home  >  Article  >  Backend Development  >  How to traverse an array in php to get different numbers

How to traverse an array in php to get different numbers

PHPz
PHPzOriginal
2023-04-27 09:03:57393browse

With the gradual development of the PHP language, array operations have become an indispensable part of our daily work. PHP provides many methods for traversing arrays, such as for, foreach loops, etc., but some developers may find it difficult when they need to count the number of different items in an array. This article will introduce several methods of traversing an array to obtain different numbers.

  1. Use for loop to traverse

Use for loop with in_array function to get the number of different items in the array. The specific implementation method is as follows:

$test_array = array("foo", "foo", "bar", "bar", "jim", "jim", "jam");
$count_array = array(); // 初始化一个存储不同项数量的数组

for ($i=0; $i<count($test_array); $i++) {
    if (!in_array($test_array[$i], $count_array)) { // 如果当前项不在count_array中
        array_push($count_array, $test_array[$i]); // 将当前项加入count_array中
    }
}
echo count($count_array); // 输出不同项的数量

The above is an implementation method, but when using a for loop, you need to consider the length of the array. For a large number of arrays, this method may bring some unnecessary time and LF.

  1. Use foreach loop to traverse

By traversing the array through foreach loop, the operation of counting the number of different items can be realized more concisely and clearly. The specific implementation method is as follows:

$test_array = array("foo", "foo", "bar", "bar", "jim", "jim", "jam");
$count_array = array(); // 初始化一个存储不同项数量的数组

foreach ($test_array as $value) {
    if (!in_array($value, $count_array)) { // 如果当前项不在count_array中
        array_push($count_array, $value); // 将当前项加入count_array中
    }
}
echo count($count_array); // 输出不同项的数量

Compared with using the for loop method, using the foreach loop requires less code, is more readable, and is relatively more efficient.

  1. Use the array_unique function

Use the array_unique function to get an array that does not contain duplicate items. The length of the array is the number of different items in the original array. The specific implementation method is as follows:

$test_array = array("foo", "foo", "bar", "bar", "jim", "jim", "jam");
$unique_array = array_unique($test_array);
echo count($unique_array);

Compared with the above two methods, the amount of code using the array_unique function is simpler and clearer, and has a more efficient processing speed.

To sum up, PHP provides a variety of methods to traverse the array to get the number of different items. When using the for loop method, you need to pay attention to the array length. For a large number of arrays, it may cause additional resource consumption. This operation can be implemented more concisely and clearly using the foreach loop method. Using the array_unique function is one of the most efficient methods and also the simplest. Readers can choose a method that suits them according to different needs.

The above is the detailed content of How to traverse an array in php to get different numbers. 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