How do I get unique values ​​from an array?

I have an array of addresses. I want to get unique address records from the array below. I used a lot of logic, but all failed to extract the unique address. If we get any differences between keys like address_one, address_two, etc. With the other, then I will consider them unique, but here some of the values ​​of the arrays are the 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'
        )

);

      

+3


source to share


2 answers


Please use this to get a unique array.



$output_array = array_unique($input_array, SORT_REGULAR);

      

+1


source


To get a unique multidimensional array, you can use this:



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

      

+5


source







All Articles