0

I have an array like this. What I am trying to do is remove duplicate entries. I want to compare values of array with different keys.

For example, the first 2 elements in array has key areacode. 3 & 4 has key state. I want to compare the values of areacode and state and remove the duplicate entries. That is, 0 and 2 have state/areacode =>'US', so I want to remove one of these.

How should I do it?

Array (
  [0] => stdClass Object
    (
        [areacode] => US
        [id] => 7-CxFsXZBGGCiYlW-NYKFw
        [city] => San Francisco
    )

  [1] => stdClass Object
    (   
        [areacode] => CL
        [id] => 7-CxFsXZBGGCiYlW-NYKFw
        [city] => San Francisco
    )

  [2]=>stdClass Object
    (    
        [state] => US
        [id] => 7-CxFsXZBGGCiYlW-NYKFw
        [city] => San Francisco
    )
  [3]=>stdClass Object
    (    
        [state] => SA
        [id] => 7-CxFsXZBGGCiYlW-NYKFw
        [city] => San Francisco
    )
)
Paul Valla
  • 517
  • 3
  • 13
Mahesh Eu
  • 515
  • 1
  • 6
  • 21
  • possible duplicate of [How to search by key=>value in a multidimensional array in PHP](http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php) – Dipesh Parmar Feb 28 '13 at 09:30

2 Answers2

0

use array_filter

$filtered = array_filter($your_array, function($obj){
  $obj_array = (array)$obj;
  return $obj_array['state'] != 'US';
});
silly
  • 7,789
  • 2
  • 24
  • 37
  • hi silly! how can i do it for my purpose? i dont have the keyword 'US' also the obj is not only state it can also be areacode. the point is there will be objects some with areacode and some with state. and there should not be same enteries in areacode and state.if there are we need to remove 1 of them – Mahesh Eu Mar 06 '13 at 12:40
0
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

Try it;i have found it here: How to remove duplicate values from a multi-dimensional array in PHP

Community
  • 1
  • 1
Razorphyn
  • 1,314
  • 13
  • 37