Domanda

After calculating distances between two points using latitude and longitude, I've created an array which looks like this:

$enterprises = array();

//Ex.: Array ( [0] => Array ( [A0A0A0] => 0 ) [1] => Array ( [A0A1A0] => 22.794344863539 ) 
for ($i=0; $i < count($cpEnterprise) ; $i++) {

        $enterprises[] = array($cpEnterprise[$i] => distance($actualCpLat, $actualCpLong, $businessLat[$i], $businessLong[$i], 'k'));

}

The main array contains the enterprises that is needed for a comparison with the actual postal codes inside it. POSTAL CODE => DISTANCE.

I need to sort those inner arrays by distance from the nearest to the farthest and I don't really understand how array_multisort works...

È stato utile?

Soluzione

An easy way to tackle this is to restructure your array and use asort

$enterprises = array();

//Ex.: Array ( [0] => Array ( [A0A0A0] => 0 ) [1] => Array ( [A0A1A0] => 22.794344863539 ) 
for ($i=0; $i < count($cpEnterprise) ; $i++) {    
        $enterprises[$cpEnterprise[$i]] = distance($actualCpLat, $actualCpLong, $businessLat[$i], $businessLong[$i], 'k');

}
asort($enterprises);

Altri suggerimenti

It depends on your sort situation to use array_multisort. I'll give my example, you might get some clues:

$products_count = array(
  2 => 10,
  5 => 20,
  0 => 13
)

$counts = array();

foreach($products_count as $k => $v)
{
  $counts[$k] = $v;
}

array_multisort($counts, SORT_NUMERIC, SORT_ASC, $products_count);

Result:

array(
  0 => 13,
  2 => 10,
  5 => 20
)

This is just an example on array_multisort and defiantly there are more better solutions and answers to your question.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top