How to sort a query listing using curl_setopt_array?

Asked

Viewed 23 times

-2

I’m populating a Dropdown, and I need to sort through Label.

curl_setopt_array($curl, array(
CURLOPT_URL => "url...,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(....

$response = curl_exec($curl);
$err = curl_error($curl);
$bairro = json_decode($response, true);

foreach ($bairro as $response) {
    $value = $response["CodBairro"];
    $label = $response["DescBairro"];
    
    $items[] = $value . '|' . $label;
}

$items = implode("\n", $items);
return $items;
curl_close($curl);

1 answer

0

You can do this using the array_sort() of php, where it is possible to clarify which is the ordering field:

//array_sort(array_a_ser_ordenado, campo_de_ordenacao, ordem_escolhida)
foreach (array_sort($bairro, 'DescBairro', SORT_ASC) as $response) {
    $value = $response["CodBairro"];
    $label = $response["DescBairro"];

    $items[] = $value.'|'.$label;
}

Obs: if you prefer you can do this already in the ordered array ($items), I don’t believe there’s any difference in performance.

$items = array_sort($items, $label, SORT_ASC);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.