PHP asort数组错误

I have the following array:

$franchise_a_status[] = array(
    'id'   => $franchise['franchise_id'],
    'text' => $franchise['franchise_surname']. ' ' .$franchise['franchise_firstname'].' '.'('.$distance.')'
);

The $franchise array is populated from the database, and the distance variable retrieves info from the Google Distance Matrix. I want the array sorted in order of distance - nearest to farthest.

I thought it was as easy as this:

asort($franchise_a_status);

Sadly I was wrong. How would I sort this array based on distance?

Using asort here won't help you. I would suggest restructuring your array and using ksort:

Untested example:

// some loop {

    $franchise_a_status[$distance . '_' . $franchise['franchise_id']] = array(
        'id'   => $franchise['franchise_id'],
        'text' => $franchise['franchise_surname'] . ' ' . $franchise['franchise_firstname'] . ' ' . '(' . $distance . ')'
    );

// }

ksort($franchise_a_status);

ksort will sort the array by key. By putting the distance at the begining of the key, the results will get sorted by distance. The id is needed as well to keep from over writing franchises when the distance away happens to be the same.

I only give you a hint: using usort() could help you.

You should write the comparison function. Won't be trivial to fetch that distance from that long string, but you stored it that way, for some reason.