Json encode return takes the asort order()

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.

Example Array, ordered, mounted with cities and my index in Table:

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

JSON example, 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"

Example of Options, generated via ajax, of the data:

<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 don't 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);
Author: William Aparecido Brandino, 2017-02-15

2 answers

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 defined order, but do not have associative indices, so when making a json_encode PHP generates a JSON in the format of object - which has no defined order.

What you can do is 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 a internal structure.

A structure in this style will maintain order.

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

Or else specifying the order:

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

EDITING REQUESTER TO SHOW RIGHT ANSWER

In assembling 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 answer.

 2
Author: Marcos, 2017-02-15 19:03:45

As @ Marcos has already commented, Your problem is not in the function json_encode, but in the way properties are indexed internally by the Javascript engine.

An alternative solution to your problem is to use an array and have multiple ordered objects inside 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);
 1
Author: jlHertel, 2017-02-15 18:37:53