如何从数组中获取唯一值?

时间:2015-08-03 15:03:44

标签: php arrays

我有地址数组。我想从下面的数组中获取唯一的地址记录。我使用了很多逻辑,但都无法提取唯一的地址。如果我们得到address_one,address_two等密钥与另一个密钥之间的任何差异,那么我会认为它是唯一的,但这里有一些数组值与其他数组相同。

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'
        )

);

2 个答案:

答案 0 :(得分:5)

要获得唯一的多维数组,您可以使用:

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

答案 1 :(得分:1)

请用它来获得独特的阵列。

$output_array = array_unique($input_array, SORT_REGULAR);