How to use ORDER BY of a data that does not come from the Bank?

0

How to use ORDER BY of a data that does not come from db? ex:

$query = $mysql->query("SELECT * FROM db_aux ;");
while($row = $query->fetch_array())
{
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=";
    $url .= $row['cep_local']."&destinations=".$row['cep_user'];
    $url .= "&language=pt&mode=car&key=AIzaSyDL6_dJ-Mbi_03_g6lHhWibxY22Z2UeYZQ";
    $json = file_get_contents($url);
    $json_data = json_decode($json, true);  
}

I want to sort by this data that comes from query json , DESC

 $json_data['rows'][0]['elements'][0]['distance']['text'];
    
asked by anonymous 20.04.2018 / 15:49

1 answer

1

With some modifications to your code and using the usort function, if you have an ordering by the desired field, for example:

<?php
    $item = array(
        array('86015-810', '86830-000'),
        array('86015-820', '86840-000'),
    );
    for($i = 0; $i < count($item); $i++)
    {
        $row = $item[$i];
        $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=";
        $url .= "{$row[0]}&destinations={$row[1]}";
        $url .= "&language=pt&mode=car&key=AIzaSyDL6_dJ-Mbi_03_g6lHhWibxY22Z2UeYZQ";
        $json = file_get_contents($url);
        $json_data[] = json_decode($json, true);
    }


    function sort_text($a, $b) {
        $f0 = str_ireplace([' ', 'km',','], ['','','.'],
                             $a['rows'][0]['elements'][0]['distance']['text']);
        $f1 = str_ireplace([' ', 'km',','], ['','','.'], 
                             $b['rows'][0]['elements'][0]['distance']['text']);

        return $f0 > $f1;
    }

    usort($json_data, 'sort_text');

    echo '<pre>';
    print_r($json_data);
    echo '</pre>';

If you can use that while of the question, but the variable must be an array that in this case is $json_data to store all the results and then do the sorting.

    
20.04.2018 / 16:40