Home  >  Article  >  Backend Development  >  How to find different values ​​in an array in php

How to find different values ​​in an array in php

PHPz
PHPzOriginal
2023-04-25 09:02:53664browse

In PHP programming, we often need to deal with arrays. During array processing, occasionally you need to find different values ​​in the array (that is, there are no repeated values). Below we will introduce several methods to help you find different values ​​in PHP arrays.

Method 1: Use the array_unique function

array_unique() function returns an array with duplicate values ​​removed. This function removes duplicate values ​​while retaining the array keys. The following is an example of using the array_unique() function:

    $arr = array(1, 2, 3, 4, 3, 5, 4, 6);
    $unique_arr = array_unique($arr);
    print_r($unique_arr);

Output:

    Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [5] => 5
        [7] => 6
    )

In this example, we use the array_unique() function to remove duplicate values ​​​​in the $arr array and get A new array $unique_arr.

However, this method has a disadvantage, that is, it can only find different values ​​in an array. If you need to find out the different values ​​in multiple arrays, you need to write additional code.

Method 2: Use array_diff function

array_diff() function is used to calculate the difference set of arrays. It will return an array containing all the elements in the $arr1 array but not in the $arr2, $arr3, etc. arrays. The following is an example of using the array_diff() function:

    $arr1 = array(1, 2, 3, 4, 5);
    $arr2 = array(3, 4, 5, 6, 7);
    $diff_arr = array_diff($arr1, $arr2);
    print_r($diff_arr);

Output:

    Array
    (
        [0] => 1
        [1] => 2
    )

In this example, we use the array_diff() function to compare the difference between the $arr1 and $arr2 arrays. A new array $diff_arr is obtained, which contains elements in the $arr1 array but not in the $arr2 array.

You can use multiple arrays as parameters of the array_diff() function, for example:

    $arr1 = array(1, 2, 3, 4, 5);
    $arr2 = array(3, 4, 5, 6, 7);
    $arr3 = array(5, 6, 7, 8, 9);
    $diff_arr = array_diff($arr1, $arr2, $arr3);
    print_r($diff_arr);

Output:

    Array
    (
        [0] => 1
        [1] => 2
    )

In this example, we compare $arr1 and $arr2 , the difference set of the $arr3 array, a new array $diff_arr is obtained, which contains the elements in the $arr1 array but not in the $arr2 and $arr3 arrays.

Method 3: Use loop search

PHP’s foreach loop is a very practical loop method. We can use foreach loop and if conditional statement to find different values ​​in an array. The following is an example of using a foreach loop and if statement:

    $arr = array(1, 2, 3, 4, 3, 5, 4, 6);
    $result_arr = array();    // 新建一个空数组
    foreach ($arr as $value)
    {
        $count = 0;           // 新建一个变量来计数
        foreach ($arr as $sub_value)
        {
            if ($value == $sub_value)
            {
                $count++;
            }
        }
        if ($count == 1)      // 如果计数为1,说明是不同的值
        {
            $result_arr[] = $value;
        }
    }
    print_r($result_arr);

Output:

    Array
    (
        [0] => 1
        [1] => 2
        [2] => 5
        [3] => 6
    )

In this example, we create a new empty array $result_arr to store different values. Use a foreach loop to iterate over the $arr array, then use another foreach loop to count the number of occurrences of the element in the array, and if the number of occurrences is 1, add the element to the $result_arr array.

The disadvantage of this method is that if the array is larger, the efficiency may be lower.

Method 4: Use the array_count_values ​​function

array_count_values() function is used to count the number of occurrences of all values ​​in the array. It returns an array whose keys are the values ​​in the $arr array and whose values ​​are the number of times that value appears in the $arr array. The following is an example of using the array_count_values() function:

    $arr = array(1, 2, 3, 4, 3, 5, 4, 6);
    $count_arr = array_count_values($arr);
    $result_arr = array();
    foreach ($count_arr as $key => $value)
    {
        if ($value == 1)
        {
            $result_arr[] = $key;
        }
    }
    print_r($result_arr);

Output:

    Array
    (
        [0] => 1
        [1] => 2
        [2] => 5
        [3] => 6
    )

In this example, we use the array_count_values() function to count the number of occurrences of each element in the $arr array, and then Use a foreach loop to traverse the result array, find the element whose occurrence count is 1, and add it to the $result_arr array.

The advantage of this method is that the code is concise, but the disadvantage is that it may consume more memory.

Conclusion

The above introduces four methods to find different values ​​​​in an array in PHP. In actual development, we need to choose the most suitable method according to the actual situation. If the array is small, you can use method three or four; if you need to find different values ​​in multiple arrays, you can use method two; if you only need to find different values ​​in one array, you can use method one.

The above is the detailed content of How to find different values ​​in an array in php. 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