Remove duplicate value from array

Hi all,

I wanted to remove some duplicate value if the key"customerid" and the key"carriername" values both are same , for example , 1169 and 1167 customerid[‘value’], carriername[‘value’] are same , so i wanted to remove 1167 from the array, may i know any function can compare two key values in array, if the same then remove it(only keep the first one)? Thanks.

1169 =>
array (size=5)
‘customerid’ => string ‘101789’
‘countryid’ => string ‘1012’
‘carriername’ => string ‘aaa’
‘shortname’ => string ‘abc’
‘code’ => string ‘111’

1168 =>
array (size=5)
‘customerid’ => string ‘812345’
‘countryid’ => string ‘2014’
‘carriername’ => string ‘aaa’
‘shortname’ => string ‘bcc’
‘code’ => string ‘122’

1167 =>
array (size=5)
‘customerid’ => string ‘101789’
‘countryid’ => string ‘8888’
‘carriername’ => string ‘aaa’
‘shortname’ => string ‘abc’
‘code’ => string ‘166’

1166 =>
array (size=5)
‘customerid’ => string ‘636212’
‘countryid’ => string ‘1012’
‘carriername’ => string ‘bbb’
‘shortname’ => string ‘abc’
‘code’ => string ‘111’

http://php.net/manual/en/function.array-unique.php

Hi Kevin Rubio,

Thank you for the reply , i have checked this page before i open this topic ,
i can see a function works for one key value,
but my situation need to compare two keys value, can you advise?
Thanks.

Brgds/Brandon Chau

Where/how does your array originate? If from a query, post your query and an sql dump of the DB. I suspect this is not actually where your problem is.

I agree. It looks like this is originating from a database.

Specifically, where is this number coming from, 1169 ?

Try this…

[php]$input = array_map(“unserialize”, array_unique(array_map(“serialize”, $input)));[/php]

I’m not that smart, I got the answer from this post… :wink:

Hi all,

Sorry , may be my question is not that clear , i’m trying to make it simple. I need a function to do 2 things .

  1. compare the key=>value (need to compare all 3 keys value; order by id > name > counting)
  2. removed the element if the key[‘id’] and key[‘name’] are same

Let’s try to break down the array in 3 phase ; 1. before sort, 2. after sort and 3. removed duplicated element

  1. before sort

array(
[1]=>Array
(
id=>666
name=>aaa
counting=>8
)

[2]=>Array
(
id=>222
name=>brandon
counting=>5
)

[3]=>Array
(
id=>111
name=>may
counting=>3
)

[4]=>Array
(
id=>555
name=>aaa
counting=>3
)

[5]=>Array
(
id=>111
name=>may
counting=>6
)

[6]=>Array
(
id=>666
name=>aaa
counting=>2
)

)

  1. after sort

array(
[1]=>Array
(
id=>666
name=>aaa
counting=>8
)
[6]=>Array
(
id=>666
name=>aaa
counting=>2
)
[4]=>Array
(
id=>555
name=>aaa
counting=>3
)
[2]=>Array
(
id=>222
name=>brandon
counting=>5
)
[5]=>Array
(
id=>111
name=>may
counting=>6
)
[3]=>Array
(
id=>111
name=>may
counting=>3
)
)

  1. removed duplicated element

array(
[1]=>Array
(
id=>666
name=>aaa
counting=>8
)

[2]=>Array
(
id=>222
name=>brandon
counting=>5
)

[5]=>Array
(
id=>111
name=>may
counting=>6
)

)

Final , foreach loop to output the array

I hope you guys can understand what i mean , thank you ^^"

Brgds/Brandon Chau

We understood what you want to do from the first post. The question we had was where does the data from your array come from?

Experience says it is likely you have work that should be done upstream of the array , and not in the array itself.

Hi ,

The array come from the MySQL command . The first page need to show all of the data(before sort) and the second page need to displayed removed duplicated element, if done by upstream of the array, i think i need to run MySQL command 2 times? I don’t want that , is possible to done by function? Thanks.

Brgds/Brandon Chau

Unless your going to provide your DB it is difficult to answer you. Should your database even have duplicate data in the first place?

The problem I see with this is, you may see duplicate data, but I do not. If you have data tied to the same user with different values, those are distinct properties, what logic would you use to say that one was more important than the other?

You could sort and then use array_unique or array_filter, but it does not solve an overall problem in a business application.

Thanks.
Brgds/Brandon Chau

Sponsor our Newsletter | Privacy Policy | Terms of Service