1

I have an array that looks like the following:

array(43197) {
 [0]=> array(4) {
     ["id"]=> string(5) "10038"
     ["country"]=> string(7) "Andorra"
     ["city"]=> string(16) "Andorra la Vella"
     ["name"]=> string(25) "Andorra la Vella Heliport" 
    }
 [1]=> array(4) {
     ["id"]=> string(5) "10040"
     ["country"]=> string(20) "United Arab Emirates"
     ["city"]=> string(17) "Abu Dhabi Emirate"
     ["name"]=> string(11) "Ras Sumeira" 
    }
 [2]=> array(4) {
     ["id"]=> string(5) "10041"
     ["country"]=> string(20) "United Arab Emirates"
     ["city"]=> string(13) "Dubai Emirate"
     ["name"]=> string(27) "Burj al Arab Resort Helipad" 
    }
 [3]=> array(4) {
     ["id"]=> string(5) "10042"
     ["country"]=> string(20) "United Arab Emirates"
     ["city"]=> string(13) "Dubai Emirate"
     ["name"]=> string(13) "Dubai Skydive" 
    }
 [4]=> array(4) {
     ["id"]=> string(5) "14243"
     ["country"]=> string(20) "United Arab Emirates"
     ["city"]=> string(13) "Dubai Emirate"
     ["name"]=> string(15) "Dubai Creek SPB" 
    }
 [5]=> array(4) {
     ["id"]=> string(5) "29266"
     ["country"]=> string(20) "United Arab Emirates"
     ["city"]=> string(17) "Abu Dhabi Emirate"
     ["name"]=> string(18) "Yas Island Airport" 
    }
...
}

Now I want to make this array 'unique' (to be able to create some select boxes later). I already have a function that works as expected... unfortunately it takes hours to complete with a very big array :(

Any ideas how to make this function faster?

function array_to_unique(//This function returns an array of unique values by given array
    //Version: 2.0.0.0
    $array,
    $uniqueCol)
    {
    $returnArray = array();

    $count = count($array);
    echo '<br>array count previous unique is: ' .$count;

    //Do the if(isset($uniqueCol)) just once - this is more code but faster with long arrays    
    if(isset($uniqueCol))
        {
        $helparray = array();
        foreach($array as $row)
            {
            if(!(in_array($row[$uniqueCol],$helparray)))
                {
                $helparray[] = $row[$uniqueCol];
                $returnArray[] = $row;
                }
            }
        }
    else{
        foreach($array as $row)
            {
            if(!(in_array($row,$returnArray)))
                {$returnArray[] = $row;}
            }
        }

    $count = count($returnArray);
    echo '<br>array count after unique is: ' .$count;

    return $returnArray;
    }

And this is how I call the function for example:

array_to_unique($array); //This is okay
array_to_unique($array,'country'); //This is very very slow

Thank you in advance

mk.maddin
  • 13
  • 3

2 Answers2

0

in_array has a complexity of O(n), meaning it has to go through all elements. This is making your code slow.

You can optimize the lookup if a value already exists, making use of a hash map instead of searching an array value. Fortunately, associative arrays in PHP are implemented like that, so we can use the value as key and make a lookup with array_key_exists.

instead of:

    $helparray = array();
    foreach($array as $row)
        {
        if(!(in_array($row[$uniqueCol],$helparray)))
            {
            $helparray[] = $row[$uniqueCol];
            $returnArray[] = $row;
            }
        }

take:

    $helparray = array();
    foreach($array as $row)
        {
        if(!(array_key_exists($row[$uniqueCol], $helparray)))
            {
            $helparray[$row[$uniqueCol]] = true;
            $returnArray[] = $row;
            }
        }

Another thing to consider with very large arrays is that you copy the values to a new array, which increases memory footprint. If it is not important for you to that the keys of the return array are 0-indexed continuous integers, you could remove duplicate items from the original array:

    $helparray = array();
    foreach($array as $key => $row)
        {
        if(!(array_key_exists($row[$uniqueCol], $helparray)))
            {
            $helparray[$row[$uniqueCol]] = true;
            }
            else
            {
            unset($array[$key]);
            }
        }
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
0
$deduplicated = [];
foreach ($array as $value) {
    $deduplicated[$value['country']] = $value;
}

Simply use the fact that keys are unique and you're automagically deduplicating your array in a single pass. If you don't like the new keys, use array_values() afterwards.

deceze
  • 510,633
  • 85
  • 743
  • 889