Return from json_encode takes the order of asort ()

2

The asort() method is used to sort arrays, without losing the index. I do the sorting, both in the SQL search and in the array. However, when sending the data via JSON (using json_encode ), it reorders the keys.

Array example, sorted, assembled with cities and my index in the table:

Array
(
    [4550] => AGUA BRANCA
    [8339] => ANADIA
    [3292] => ARAPIRACA
    [7509] => ATALAIA
    [21265] => BARRA DE SANTO ANTONIO
    [9109] => BARRA DE SAO MIGUEL
    ...

Example of JSON, shown in browser PREVIEW (still ordered):

"cidades":{"4550":"AGUA BRANCA","8339":"ANADIA","3292":"ARAPIRACA","7509":"ATALAIA","21265":"BARRA DE SANTO ANTONIO","9109":"BARRA DE SAO MIGUEL","6127"

Examples of ajax generated options:

<option value="314">MACEIO</option>
<option value="872">PILAR</option>
<option value="1145">UNIAO DOS PALMARES</option>
<option value="1432">RIO LARGO</option>
<option value="1647">MARIBONDO</option>
<option value="1648">SAO MIGUEL DOS CAMPOS</option>
<option value="1845">PARIPUEIRA</option>

On receipt of ajax , I do not ask to sort by key, which is what happens. The generation of my SELECT is simply:

$.each(data.cidades,function(k,v){
    options += "<option value='"+k+"'>"+v+"</option>";
});
$("#consumidor_cidade").html(options);
    
asked by anonymous 15.02.2017 / 18:59

2 answers

1

The problem is not in json_encode , as you yourself showed in the preview comes in the correct order.

The problem happens because in JavaScript only arrays have a defined order, but they do not have associative indexes, so when doing a json_encode PHP generates a JSON in the object format - which has no defined order.

What you can do is to send one more attribute in the list with the order, or leave the index of the sequential array in the order you want to display and save the id and name in an internal structure.

A structure in this style will keep order.

$arr = [
  ['codigo'=>'4550', 'nome'=>'AGUA BRANCA'],
  ['codigo'=>'8339', 'nome'=>'ANADIA']
]

Or by specifying the order:

$arr = [
  ['codigo'=>'4550', 'nome'=>'AGUA BRANCA', 'order'=>0],
  ['codigo'=>'8339', 'nome'=>'ANADIA', 'order'=>1]
]

REQUIRED ISSUE FOR CERTAIN RESPONSE SHOW

In the assembly of my array, it was done like this:

while ($resultado = pg_fetch_object($res)) {
    $cidades[] = array("cidade_id" => $resultado->cidade, "cidade_nome" => $resultado->cidade_nome);
}
/*envio*/
return json_encode(array("ok"=>true,"cidades" => $cidades));

And assembly by jQuery was described in Hertel's response.

    
15.02.2017 / 19:16
1

Since @Marcos has already commented, your problem is not in the json_encode function, but in how properties are indexed internally by the Javascript engine.

A workaround for your problem is to use an array and have multiple objects sorted within it:

<?php
$array = [
   [
     "indice" => 4550,
     "nome"   => "AGUA BRANCA"
   ],
   [
     "indice" => 8339,
     "nome"   => "ANADIA"
   ]
];

And in javascript do:

$.each(data.cidades,function(k,v){
    options += "<option value='"+v.indice+"'>"+v.nome+"</option>";
});
$("#consumidor_cidade").html(options);
    
15.02.2017 / 19:37