Domanda

I am trying to order an array produced by a foreach loop, here is my code:

$lowestvar = array();

foreach ($variations as $variation){
    $lowestvar[] = $variation['price_html'];            
}

I am then using array_multisort like this:

array_multisort($lowestvar, SORT_ASC);
print_r($lowestvar);

This works for the first looped item with a output of:

Array ( [0] => £10.00 [1] => £15.00 ) 

But the second array in the loop looks like this:

Array ( [0] => £10.00 [1] => £5.00 ) 

Any ideas on where i am going wrong?

È stato utile?

Soluzione 2

You can use usort() as like in the following example

 function cmp($a1, $b1)
 {
  $a=str_replace('£','',$a1);
  $b=str_replace('£','',$b1);
  if ($a == $b) {
      return 0;
    }
   return ($a < $b) ? -1 : 1;
 }

  $a = array('£10.00','£5.00');

  usort($a, "cmp");
  print_r($a);

Output

Array
(
  [0] => £5.00
  [1] => £10.00
 )

Altri suggerimenti

You're sorting STRINGS, which means that 10 < 5 is true. Remember that string sorting go char-by-char, not by "entire value".

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