1

I wish classify the values of my table in ascending order to be able to use them in a variable $distancecp. My var_dump finds my values well but I can not classify them in ascending order.

$select100=mysqli_query($conn,$select10);
while($asso = mysqli_fetch_assoc($select100)) {
    $distancecp1 = getDistance(''.$villeselect.', '.$cpselect.'',''.$asso['ville'].', '.$asso['codep'].'');
    $distancecp2 = array($distancecp1);
    var_dump($distancecp2);
    foreach($distancecp2 as $distancecp) {
    }
}

Results of var_dump($distancecp2) :

array (size=1)
  0 => 
    object(SimpleXMLElement)[8]
      public 0 => string '68526' (length=5)

array (size=1)
  0 => 
    object(SimpleXMLElement)[10]
      public 0 => string '71824' (length=5)

array (size=1)
  0 => 
    object(SimpleXMLElement)[7]
      public 0 => string '67536' (length=5)

array (size=1)
  0 => 
    object(SimpleXMLElement)[9]
      public 0 => string '33902' (length=5)

I tried :

$select100=mysqli_query($conn,$select10);
while($asso = mysqli_fetch_assoc($select100)) {
$distancecp1 = getDistance(''.$villeselect.', '.$cpselect.'',''.$asso['ville'].', '.$asso['codep'].'');
$distancecp2 = array($distancecp1);
asort($distancecp2);
foreach($distancecp2 as $distancecp){
    echo ''.$distancecp.' ';
}
}

My echo returns me well my 4 values but not ranked in ascending order :(

Cœur
  • 32,421
  • 21
  • 173
  • 232
Chrys
  • 79
  • 1
  • 8
  • For starters, there's no point in putting a single item into an array and then doing a `foreach` loop over it. Second, what have you tried to solve this problem? – miken32 Feb 01 '17 at 20:07
  • Possible duplicate of [Basic simpleXML working example?](http://stackoverflow.com/questions/1893024/basic-simplexml-working-example) – miken32 Feb 01 '17 at 20:08
  • @miken32 Yes I have tried to solve the problem it is 2 weeks that I pluck my hair on this problem of which I can not find the solution :/ Now I am bald lol – Chrys Feb 01 '17 at 20:10

1 Answers1

1

Look carefully at your var_dump output: it's not printing a list of all your results, but is called multiple times, each time saying "array (size=1)". That "size=1" is your clue: you have a list with one thing in it, created with array($something). If you sort a list with one thing in it, you will just get the same list, with the same thing in it.

What you need to do instead is create one array for the whole loop, and add all the items to it:

$results = array();
while ( ... ) {
    $distancecp1 = ...
    $results[] = $distancecp1;
}
var_dump($results);

Then:

Community
  • 1
  • 1
IMSoP
  • 65,743
  • 7
  • 83
  • 127