0

First of all this is not a duplicate question as i have tried most of the stack answer.

I have about 138886 records in my array.

records are like

[1] => Array
        (
            [country] => US
            [state] => Albama
            [city] => Brest
            [postcode] => 225001-225003
            [shipping_info] => Delivery Available
            [is_zip_range] => 1
            [zip_from] => 225001
            [zip_to] => 225003
        )

    [2] => Array
        (
            [country] => BY
            [state] => Brest
            [city] => Brest
            [postcode] => 225001-225003
            [shipping_info] => Delivery Available
            [is_zip_range] => 1
            [zip_from] => 225001
            [zip_to] => 225003
        )

I want to unique all record from postcode value i have tried some method are

Method 1

$temp = array_unique(array_column($data, 'postcode'));
$filteredData = array_intersect_key($data, $temp);

but it is still giving duplicate value.

Method 2

$filteredData = array_map("unserialize", array_unique(array_map("serialize", $data)));   

this is won't work

Method 3

function unique_multidim_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 

    foreach($array as $val) { 
        if (!in_array($val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
        } 
        $i++; 
    } 
    return $temp_array; 
} 
$details = unique_multidim_array($array,'postcode');

It is working but too slow taking about 2/3 mins.

let me know any other method i can use for unique array. help will be apprecitaed

Himanshu
  • 251
  • 4
  • 18

1 Answers1

1

Why are you making it so hard,

 $in = array (
    1 => 
    array (
        'country' => 'US',
        'state' => 'Albama',
        'city' => 'Brest',
        'postcode' => '225001-225003',
        'shipping_info' => 'DeliveryAvailable',
        'is_zip_range' => 1,
        'zip_from' => 225001,
        'zip_to' => 225003
    ),
    2 => 
    array (
        'country' => 'BY',
        'state' => 'Brest',
        'city' => 'Brest',
        'postcode' => '225001-225003',
        'shipping_info' => 'DeliveryAvailable',
        'is_zip_range' => 1,
        'zip_from' => 225001,
        'zip_to' => 225003
    )
);

$out = [];
foreach($in as $i) if(!isset($out[$i['postcode']])) $out[$i['postcode']] = $i;

Sandbox

You can do the same thing with in_array, but the isset is faster.

In fact you dont even need to do the isset

foreach($in as $i) $out[$i['postcode']] = $i;

Array keys are always unique, but this will retain the last duplicate where as the previous code keeps the first one.

And if the keys bug you latter just do $out = array_values($out) to reset them.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • 1
    It has no choice but to work. There is no need for your inner loop over the "fields" as you already know what field you want, `want to unique all record from postcode value` Point in fact you don't even need to do isset because the next duplicate will overwrite the previous, it just depends what you want first. – ArtisticPhoenix Nov 01 '18 at 11:11