0

I can't figure out why asort isn't working. Neither does any other sort work. $hs['hs_type'] are values that come from MySQL query.

$results = $query->result_array();
$hs_types = array();
foreach($results as $hs) {
    $hs_types[$hs['hs_type']]++;
}

$projects = array();
foreach($hs_types as $hs) {
    array_push($projects, $hs);
}
asort($projects);

var_dump for my array before sort: array (size=15)

  * 8 => int 1709
  * 13 => int 26
  * 7 => int 474
  * 14 => int 800
  * 11 => int 282
  * 6 => int 61
  * 5 => int 23
  * 15 => int 181
  * 3 => int 2
  * 19 => int 3
  * 9 => int 50
  * 1 => int 44
  * 2 => int 2
  * 4 => int 4
  * 18 => int 13

var_dump for my array after sort: array (size=15)

  * 8 => int 2
  * 12 => int 2
  * 9 => int 3
  * 13 => int 4
  * 14 => int 13
  * 6 => int 23
  * 1 => int 26
  * 11 => int 44
  * 10 => int 50
  * 5 => int 61
  * 7 => int 181
  * 4 => int 282
  * 2 => int 474
  * 3 => int 800
  * 0 => int 1709

What I wanted:

  * 3 => int 2
  * 2 => int 2
  * 19 => int 3
  * 4 => int 4
  * 18 => int 13
  * 5 => int 23
  * 13 => int 26
  * 1 => int 44
  * 9 => int 50
  * 15 => int 181
  * 11 => int 282
  * 7 => int 474
  * 14 => int 800
  * 8 => int 1709
1
  • 1
    Show your sort code as well please. Commented Feb 20, 2014 at 13:57

3 Answers 3

1

Problem is that you are getting everything into your nicely keyed array, then pushing each item into the $projects array (losing the key) then doing a asort on the $projects array.

It shows up with this minor test script:-

<?php

$hs_types = array(8 =>  1709,
   13 =>  26,
   7 =>  474,
   14 =>  800,
   11 =>  282,
   6 =>  61,
   5 =>  23,
   15 =>  181,
   3 =>  2,
   19 =>  3,
   9 =>  50,
   1 =>  44,
   2 =>  2,
   4 =>  4,
   18 =>  13);

Echo "\r\nOriginal array\r\n";
print_r($hs_types);

$projects = array();
foreach($hs_types as $hs) 
{
    array_push($projects, $hs);
}

asort($projects);

Echo "\r\nPushed array sorted\r\n";
print_r($projects);

asort($hs_types);

Echo "\r\nOriginal array sorted\r\n";
print_r($hs_types);

?>

but it looks like it would be easier to just get the sorted list in SQL in the first place.

Sign up to request clarification or add additional context in comments.

Comments

0

I tried using your array with asort() and it worked perfectly, coming back with the result you expected:

<?php
//your array
$a = array(
    8 =>  1709,
    13 =>  26,
    7 =>  474,
    14 =>  800,
    11 =>  282,
    6 =>  61,
    5 =>  23,
    15 =>  181,
    3 =>  2,
    19 =>  3,
    9 =>  50,
    1 =>  44,
    2 =>  2,
    4 =>  4,
    18 =>  13
);

//print unsorted array
print_r($a);

//sort the array
asort($a);

//print the sorted array
echo "<br/><br/>";
print_r($a);

If this really isn't working for you, why not do the sorting in the SQL query instead? Then you wouldn't need to bother re-sorting it in PHP.

Comments

0

Are you sure you are using asort instead of sort ? I tried your code and it works perfectly :

<?php 

$myArray = array(
    8 => 1709,
    13 => 26,
    7 => 474,
    14 => 800,
    11 => 282,
    6 => 61,
    5 => 23,
    15 => 181,
    3 => 2,
    19 => 3,
    9 => 50,
    1 => 44,
    2 => 2,
    4 => 4,
    18 => 13,
);

var_dump($myArray);

asort($myArray);
var_dump($myArray);
?>

My result :

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.