I'm using this:

$result = mysql_query($_SESSION[$search.'sql']) or trigger_error("SQL", E_USER_ERROR); 
while ($list = mysql_fetch_assoc($result)) { 

if($_POST['submit'] == "Show Distance"){ 
//If user has set postcode create distance info. 
    //Users postcode 
    $pcodeA = strstr($_SESSION['postcode'], ' ', true); 
    //Database Postcode 
        $pcodeB = strstr($row['postcode'], ' ', true); 
    //distance variable 
    $distance = calc_postcode_seperation($pcodeA,$pcodeB); 
}else{ 
//User hasn't submitted distance so distance variable is blank 
    $distance = ""; 
}; 


$data[] = array('name' => $list['name'], 'distance' => $distance); 
    }; 

// Obtain a list of columns 
    foreach ($data as $key => $row) { 
        $name[$key]  = $row['name']; 
    $distance[$key] = $row['distance']; 
} 

// Sort the data with volume descending, edition ascending 
// Add $data as the last parameter, to sort by the common key 
array_multisort($distance, SORT_DESC, $name, SORT_ASC, $data); 

foreach($data as &$list){ 
    echo $list['name']."<br>"; 
    echo $list['distance']."miles"; 
}; 
}; 

and getting this

Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in etc

Does anyone know whats going on??

    PS. I think it might be due to $destination being the same on several rows but i can't work out how to overcome it.

      The error is indicating that your $distance is not actually an array, therefore it cannot be sorted.

      If it's there is no instance where it should not be an array, you have an error somewhere else when you build the array. If there is a chance that it is empty sometimes, just add a check to make sure it's an array before you try and sort it.

      if(is_array($distance)){
         array_multisort($distance, SORT_DESC, $name, SORT_ASC, $data); 
      }
      

      If it should be an array, add some echo statements in your loops that are buliding it, make sure it looks right.

        Do you think that it could be because $distance isn't drawn from any database, it's a mathematical function

          Ok so ive tested whether or not it's an array and it's not reading it as an array, however, all the values are being echoed ok and repeated but its not ordering by it. ive tested ordering by name and it's working.

            SORTED

            i had to change it from this

            while ($list = mysql_fetch_assoc($result)) { 
            
            if($_POST['submit'] == "Show Distance"){ 
            //If user has set postcode create distance info. 
                //Users postcode 
                $pcodeA = strstr($_SESSION['postcode'], ' ', true); 
                //Database Postcode 
                    $pcodeB = strstr($row['postcode'], ' ', true); 
                //distance variable 
                $distance = calc_postcode_seperation($pcodeA,$pcodeB); 
            }else{ 
            //User hasn't submitted distance so distance variable is blank 
                $distance = ""; 
            }; 
            
            
            $data[] = array('name' => $list['name'], 'distance' => $distance); 
                }; 
            

            to this:

            while ($list = mysql_fetch_assoc($result)) { 
            
            if($_POST['submit'] == "Show Distance"){ 
            //If user has set postcode create distance info. 
                //Users postcode 
                $pcodeA = strstr($_SESSION['postcode'], ' ', true); 
                //Database Postcode 
                    $pcodeB = strstr($row['postcode'], ' ', true);  
            
            }else{ 
            }; 
            
            
            $data[] = array('name' => $list['name'], 'distance' => calc_postcode_seperation($pcodeA,$pcodeB);); 
                }; 
            
              	# I don't know what calc_postcode_separation returns, but I was guessing int or float.
                  # And correcting the spelling misstake is probably a good idea: separation, not seperation
              	# So, $distance is either int/float, or empty string - NOT an array
                  $distance = "";
                  $data[] = array('name' => $list['name'], 'distance' => $distance);
              };
              
              # Below, $distance is still not an array. It is int/string/float
              # If it is int/float, you can't use it as an array.
              # If it is a string, you can use array notation for string character indexing,
              # but since it would then be an empty string, it contain no characters = problem
              # Solution: turn it into an array
              $distance = array();
              foreach ($data as $key => $row) {
              	$distance[$key] = $row['distance'];
              }
              
              # But before you add $distance = array() above to your code, first put this line here
              var_dump($distance);
              # and it will show you that $distance still is no array. Hence the error message for
              # your call to array_multisort
              # Making use of var_dump, print_r, echo and/or error_log is the only way you'll
              # find errors yourself sometimes. So make heavy use of them for debugging. Once
              # you are absolutely certain what isn't what you intended it to be, you can start figuring
              # out why.
              
              // Sort the data with volume descending, edition ascending
              // Add $data as the last parameter, to sort by the common key
              array_multisort($distance, SORT_DESC, $name, SORT_ASC, $data);
              

              If you had used some actual distances, you should have gotten another error than about array_multisort. Something like "can't use scalar as array", assuming your distance is an int or float.
              It is ok to use array notation for string character indexing.

              $s = 'abcd';
              $s[3] = 'a';
              echo $s;    // abca
              
              $t = 'a';
              $s[3] = 'a';
              echo $s;    // a  a
              

              However, doing this on an empty string actually turns it into an array. So, I'm guessing you do not return int/float from your calc_postcode_separation, but rather a numerical string. Or that you tried only without an actual distance, but weren't really using an empty string, but rather something like " ".

              And finally I'd like to suggest this simplification

              while () {
              	// as before...
              
              # set up both $data, $name and $distance here
              # to save yourself looping over the same data twice
              $data[] = array('name' => $list['name'], 'distance' => $dist);
              $name[]  = $list['name'];
              $distance[] = $dist;
              }
              # which of course means removing the foreach ($data as $key => $row) here
              # also, if you didn't make this modification, I'd suggest dropping $key => from the
              # foreach, since you do not need the $key. The foreach loop could just as well have been
              foreach ($data as $row) {
              	$name[] = $row['name'];
              	$distance[] = $row['distance'];
              }
              

              Hmm, another finally... Make sure you indent your code properly. My first though was that your code ran the foreach loop within the while loop, since your code was indented like this

              # not wrong, but definitely not good either
              while () {
              	# lines of code
              	}	// ending while in a non obvious way
              
              # more lines of code, also suggesting that the while loop has not been ended
              
              # good
              while () {
              	# lines of code
              
              }	// ending the code block at the same level of indentation as it was started
              
              # more lines of code, also continuing at the same indentation level
              # as the above block was started
              

                wow, a lot to mull over. Cheers for an in-depth answer!

                  Write a Reply...