Vikas Burman
Vikas Burman

Reputation: 355

How to get unique values from an array?

I have address array. I want to get unique address records from below array. I have used lots of logic but all fails to extract unique address. If we get any differences between keys like address_one, address_two etc. with another one, then I will consider as unique, but here some array values same with others.

Array
(    
0 => Array
        (
            'address_one' => 'qqqqqqqqqq',
            'address_two' => 'wwwwww',
            'zipcode' => '212121',
            'country_id' => '1',
            'country_name' => 'United States',
            'state_id' => '5',
            'state_name' => 'AP',
            'city_id' => '3',
            'city_name' => 'Bhopal'
        ),    
1 => Array
        (
            'address_one' => 'lkl',
            'address_two' => 'ik2',
            'zipcode' => '564564',
            'country_id' => '1',
            'country_name' => 'United States',
            'state_id' => '1',
            'state_name' => 'Madhya Pradesh',
            'city_id' => '1',
            'city_name' => 'Indore'
        ),    
2 => Array
        (
            'address_one' => 'ace1',
            'address_two' => 'caldrys1',
            'zipcode' => '564561',
            'country_id' => '91',
            'country_name' => 'Guinea',
            'state_id' => '3',
            'state_name' => 'AL',
            'city_id' => '3',
            'city_name' => 'Bhopal'
        ),    
3 => Array
        (
            'address_one' => '',
            'address_two' => '',
            'zipcode' => '',
            'country_id' => '',
            'country_name' => '',
            'state_id' => '',
            'state_name' => '',
            'city_id' => '',
            'city_name' => ''
        ),    
4 => Array
        (
            'address_one' => '',
            'address_two' => '',
            'zipcode' => '',
            'country_id' => '',
            'country_name' => '',
            'state_id' => '',
            'state_name' => '',
            'city_id' => '',
            'city_name' => ''
        ),    
5 => Array
        (
            'address_one' => 'lkl',
            'address_two' => 'ik2',
            'zipcode' => '564564',
            'country_id' => '1',
            'country_name' => 'United States',
            'state_id' => '1',
            'state_name' => 'Madhya Pradesh',
            'city_id' => '1',
            'city_name' => 'Indore'
        ),    
6 => Array
        (
            'address_one' => 'Ace',
            'address_two' => 'Matru Line',
            'zipcode' => '483504',
            'country_id' => '100',
            'country_name' => 'India',
            'state_id' => '1',
            'state_name' => 'Madhya Pradesh',
            'city_id' => '2',
            'city_name' => 'Katni'
        ),    
7 => Array
        (
            'address_one' => 'lkl',
            'address_two' => 'ik2',
            'zipcode' => '564564',
            'country_id' => '1',
            'country_name' => 'United States',
            'state_id' => '1',
            'state_name' => 'Madhya Pradesh',
            'city_id' => '1',
            'city_name' => 'Indore'
        ),
8 => Array
        (
            'address_one' => 'ace1',
            'address_two' => 'caldrys1',
            'zipcode' => '564561',
            'country_id' => '91',
            'country_name' => 'Guinea',
            'state_id' => '3',
            'state_name' => 'AL',
            'city_id' => '3',
            'city_name' => 'Bhopal'
        )

);

Upvotes: 3

Views: 71

Answers (2)

Amar Kalra
Amar Kalra

Reputation: 51

Please use this to get unique array.

$output_array = array_unique($input_array, SORT_REGULAR);

Upvotes: 1

Ravi Tiwari
Ravi Tiwari

Reputation: 73

To get unique multidimensional array you can use this :

$output_array = array_map(
    "unserialize",
    array_unique(array_map("serialize", $input_array))
);

Upvotes: 5

Related Questions