php array converted to json does not maintain order in mysql

Asked

Viewed 160 times

1

I have an array with the following format:

$vetor = array(

   1=>array(
      pt-BR => array(
       'pergunta1' => 'pergunta 1 em texto'
       'resposta1' => 'resposta 1 em texto'
         )
      )
   3=>array(
      pt-BR => array(
       'pergunta3' => 'pergunta 3 em texto'
       'resposta3' => 'resposta 3 em texto'
         )
      )
);

I am trying to save this array to a json dynamic column in mysql 5.7. How can I keep order by putting question 3 before 1 and keep that order?

  • 1

    How about "3 before 1" and "keep that order"?

  • Descending order?

  • The order I set it. It does not maintain the order and orders it crescently.

  • Because it’s a array. If you need it ordered, you will have to sort it when using the data.

2 answers

0

To sort an array down you must use the function krsort, but this function will keep the original keys. If you want to ensure this new order, you can use array_values to get a copy of the array with new keys.

$vetor = [
    1 => [
        'pt-BR' => [
            'pergunta1' => 'pergunta 1 em texto',
            'resposta1' => 'resposta 1 em texto'
        ]
    ],
    3 => [
        'pt-BR' => [
            'pergunta3' => 'pergunta 3 em texto',
            'resposta3' => 'resposta 3 em texto'
        ]
    ]
];

krsort($vetor);// ordena de forma decrescente
$vetor = array_values($vetor);// extrai os valores do array e cria novas chaves para ele
var_dump($vetor);

$vetor = json_encode($vetor);// json na ordem desejada
var_dump($vetor);

The result of the first var_dump then it will be:

array(2) {
  [0]=>
  array(1) {
    ["pt-BR"]=>
    array(2) {
      ["pergunta3"]=>
      string(19) "pergunta 3 em texto"
      ["resposta3"]=>
      string(19) "resposta 3 em texto"
    }
  }
  [1]=>
  array(1) {
    ["pt-BR"]=>
    array(2) {
      ["pergunta1"]=>
      string(19) "pergunta 1 em texto"
      ["resposta1"]=>
      string(19) "resposta 1 em texto"
    }
  }
}

And the second var_dump after the json_encode will return:

string(161) "[{"pt-BR":{"pergunta3":"pergunta 3 em texto","resposta3":"resposta 3 em texto"}},{"pt-BR":{"pergunta1":"pergunta 1 em texto","resposta1":"resposta 1 em texto"}}]"
  • 1

    Please see [Answer], include a minimal and verifiable example in your reply. Responses containing only link are not very useful, due to the link might get OFF

  • The problem is when recording in the bank, Azer is to does not guarantee the order when recorded

  • So the solution is you save in the database in ascending order even, and use this function that I showed you when displaying the data.

  • I typed by cell phone and cut the text, is the following, does not compensate to adjust, when it use json_encode to record in the database the ordering may not be preserved and at the time of reading this is also lost sometimes, outside that to iterate (for, while, etc) Being ordained doesn’t make much difference if you use $i++, as I said in my reply.

  • Then the solution would be to create a copy of the array with new keys before json_encode, this would ensure the new order when entering into the database. And when displaying rescued database data just use a foreach.

0

It doesn’t matter the order that is recorded, at the time of decoding (when it does the SELECT and then uses json_decode) is likely that order be lost again, no need to worry about the "rescue" moment, worry about the time after "decode" to display, you can resolve at the time of reading easily on for

$obj = json_decode($linha['dadosjsonnobanco'], true);

//Pega a maior chave no seu array
$higher = max(array_keys($obj));

for ($i = $higher; $i >= 0; $i--) {
     if (empty($obj[$i])) continue; //Se não existir um item pula para o proximo

     var_dump($obj[$i]);
}

See that instead of using ++ I reversed, it’s --, so you start with the largest number, it starts with, say, 3, so it goes to 2 and since 2 doesn’t exist, it fires continue;, then go to 1 then to the 0, like the 0 does not exist it ends the for

Browser other questions tagged

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