title: PHP - array to 'unique'
body: <p>I have an array that looks like the following:</p>
  
  <pre><code>array(43197) {
   [0]=&gt; array(4) {
       ["id"]=&gt; string(5) "10038"
       ["country"]=&gt; string(7) "Andorra"
       ["city"]=&gt; string(16) "Andorra la Vella"
       ["name"]=&gt; string(25) "Andorra la Vella Heliport" 
      }
   [1]=&gt; array(4) {
       ["id"]=&gt; string(5) "10040"
       ["country"]=&gt; string(20) "United Arab Emirates"
       ["city"]=&gt; string(17) "Abu Dhabi Emirate"
       ["name"]=&gt; string(11) "Ras Sumeira" 
      }
   [2]=&gt; array(4) {
       ["id"]=&gt; string(5) "10041"
       ["country"]=&gt; string(20) "United Arab Emirates"
       ["city"]=&gt; string(13) "Dubai Emirate"
       ["name"]=&gt; string(27) "Burj al Arab Resort Helipad" 
      }
   [3]=&gt; array(4) {
       ["id"]=&gt; string(5) "10042"
       ["country"]=&gt; string(20) "United Arab Emirates"
       ["city"]=&gt; string(13) "Dubai Emirate"
       ["name"]=&gt; string(13) "Dubai Skydive" 
      }
   [4]=&gt; array(4) {
       ["id"]=&gt; string(5) "14243"
       ["country"]=&gt; string(20) "United Arab Emirates"
       ["city"]=&gt; string(13) "Dubai Emirate"
       ["name"]=&gt; string(15) "Dubai Creek SPB" 
      }
   [5]=&gt; array(4) {
       ["id"]=&gt; string(5) "29266"
       ["country"]=&gt; string(20) "United Arab Emirates"
       ["city"]=&gt; string(17) "Abu Dhabi Emirate"
       ["name"]=&gt; string(18) "Yas Island Airport" 
      }
  ...
  }
  </code></pre>
  
  <p>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 :(</p>
  
  <p>Any ideas how to make this function faster?</p>
  
  <pre><code>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 '&lt;br&gt;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 '&lt;br&gt;array count after unique is: ' .$count;
  
      return $returnArray;
      }
  </code></pre>
  
  <p>And this is how I call the function for example:</p>
  
  <pre><code>array_to_unique($array); //This is okay
  array_to_unique($array,'country'); //This is very very slow
  </code></pre>
  
  <p>Thank you in advance</p>