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