比较一个数组和输出的值

时间:2022-06-17 21:43:18

i have an array that can contain the same value more than once. What I'm trying to find is a way to compare the values inside that array and output every value only once.

我有一个数组,它可以包含相同的值不止一次。我要找的是一种比较数组内的值并只输出一个值的方法。

$_just_a_tier = array();

foreach ($_associatedProducts as $_item){
// count all products

    $_tierprice = $this->getTierPrices($_item);
     foreach ($_tierprice as $_ay){                              
                $_tier = $_ay['price_qty'];                              
               // echo $_tier.' | ';

                $_just_a_tier[] = $_tier;
      }
}

print_r($_just_a_tier);

this will output e.g. that:

这将输出,例如:

Array
(
    [0] => 36
    [1] => 50
    [2] => 72
    [3] => 108
    [4] => 110
    [5] => 120
    [6] => 144
    [7] => 180
    [8] => 360
    [9] => 540
    [10] => 960
    [11] => 20
    [12] => 30
    [13] => 36
    [14] => 72
    [15] => 108
    [16] => 144
    [17] => 180
    [18] => 360
    [19] => 540
)

Thanks.

谢谢。

2 个解决方案

#1


5  

There is a built-in function in PHP called array_unique that solves this problem for you.

PHP中有一个名为array_unique的内置函数可以为您解决这个问题。

#2


1  

Have you looked at PHP's array_unique() function?

您看过PHP的array_unique()函数了吗?

#1


5  

There is a built-in function in PHP called array_unique that solves this problem for you.

PHP中有一个名为array_unique的内置函数可以为您解决这个问题。

#2


1  

Have you looked at PHP's array_unique() function?

您看过PHP的array_unique()函数了吗?