# 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