简体   繁体   English

如何检查数组中的某些值是否相同

[英]How to check if some values in array are the same

I want to check for identical values in an array 我想检查数组中的相同值

I have the next array 我有下一个数组

array(3) { 
    [0]=> array(3) { 
        [0]=> string(1) "1" 
        [1]=> int(349) 
        [2]=> int(4) } 
    [1]=> array(3) { 
        [0]=> string(1) "2" 
        [1]=> int(429) 
        [2]=> int(3) }
    [2]=> array(3) { 
        [0]=> string(1) "3" 
        [1]=> int(429) 
        [2]=> int(2) } 
}

What I want is to chek if in this array two or more of the values with the key [1] are the same, in this case two that are identical, the number 429. In the case that two or more are identical, I want to check the value of key[2] to see what is bigger and make something. 我要检查的是,如果在此数组中,具有键[1]的两个或多个值相同,在这种情况下,两个相同,即数字429。在两个或多个相同的情况下,我想要检查key [2]的值以查看更大的值并做一些事情。

I can check if two values are identical but i don't know how to check the other value (key[2]) and get if it is bigger or lower. 我可以检查两个值是否相同,但是我不知道如何检查另一个值(键[2])并确定它是否更大或更小。

Since you didn't demonstrate any attempts to solve that on your own, I'm answering in pseudocode: 由于您没有展示任何尝试自行解决的尝试,因此我以伪代码回答:

dict = empty array
for each item in your array
   let key be item[1]
   if exists dict[key]
       compare item[2] and dict[key][2]
   otherwise
       let dict[key] be item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM