简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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