title: php arrays, storing multiple values with the same key within foreach loop
body: <p>I believe what I'm trying to accomplish is straight forward.</p>
  
  <p>Some context, the code below simple pushes a city name ($cityProx) into a single dimension array($validLocationCity) if the a given distance is less a given proximity distance($prox)</p>
  
  <pre><code>if($actualDistance &lt;= $prox)
  {   
      array_push($validLocationCity,$cityProx);
  }
  </code></pre>
  
  <p>So this is fine. 
  But now I wish to sort this array of cities based on the distance.</p>
  
  <p>My theory for a simple way to accomplish this, is to store the distance as the key in the array, allowing me to use ksort(), this will allow me to loop through the cities, nearest first at a later point. </p>
  
  <pre><code>if($actualDistance &lt;= $prox)
  {   
      $validLocationCitySortDistance[$actualDistance] = $cityProx;
  }
  </code></pre>
  
  <p>Above was my first attempt at accomplishing this, but, of course, any city with same distance will overwrite the previous value stored for that distance/key.</p>
  
  <p>How can I implement this so that if the keys are the same, ie equal distance, the new value(city name) will simply be added/appended to that key.
  Also assume the distance will be equal in more than 1 case.</p>
  
  <p>// End question<br>
  //--------------------------------->></p>
  
  <p>I attempted adding a new array as the value if for a given key was taken </p>
  
  <pre><code>if($validLocationCitySortDistance[$actualDistance] != null) 
  {
      $validLocationCitySortDistance[$actualDistance] [] = $cityProx;
  }
  else
  {
       $validLocationCitySortDistance[$actualDistance] = $cityProx;
  }
  </code></pre>
  
  <p>This appears to be an incorrect and overly complicated way to do what I'm trying to do. Since I don't know how many times I need to iterate, this method sucks. 
  I'll admit, I think I might be just misunderstanding what exactly I'm doing in the latter example above.</p>
  
  <p>If somebody could help it would really appreciated, I'd also ask if you see a quick solution to also show how I might grab the values from your solution using a foreach or the likes. </p>
  
  <p>Hope you get me,<br>
  Thanks in advance! </p>