PHP - array to 'unique' -


i have array looks 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 want make array 'unique' (to able create select boxes later). have function works expected... unfortunately takes hours complete big array :(

any ideas how make function faster?

function array_to_unique(//this function returns array of unique values given array     //version: 2.0.0.0     $array,     $uniquecol)     {     $returnarray = array();      $count = count($array);     echo '<br>array count previous unique is: ' .$count;      //do if(isset($uniquecol)) once - more code faster long arrays         if(isset($uniquecol))         {         $helparray = array();         foreach($array $row)             {             if(!(in_array($row[$uniquecol],$helparray)))                 {                 $helparray[] = $row[$uniquecol];                 $returnarray[] = $row;                 }             }         }     else{         foreach($array $row)             {             if(!(in_array($row,$returnarray)))                 {$returnarray[] = $row;}             }         }      $count = count($returnarray);     echo '<br>array count after unique is: ' .$count;      return $returnarray;     } 

and how call function example:

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

thank in advance

$deduplicated = []; foreach ($array $value) {     $deduplicated[$value['country']] = $value; } 

simply use fact keys unique , you're automagically deduplicating array in single pass. if don't new keys, use array_values() afterwards.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -