mysqli

SELECT ... ,concat('[[', GROUP_CONCAT(`id`, ',', '\"',`name`, '\"' SEPARATOR '],['),']]') as npc 

A string of the following type arrives from the base (json):

 var_dump($row['npc']);//→ string(62) "[[679,"<Привратник> Хранитель Храма"]]" 

I want to convert this string to an array, but I can't

 $row['npc'] = json_decode($row['npc']);// пусто 

If to copy a line from the browser and to substitute in json_decode

 $row['npc'] = json_decode('[[679,"<Привратник> Хранитель Храма"]]'); 

then convert it turns out. What kind of nonsense I do not understand: (

1 answer 1

If json_decode returned void, then an error occurred during decoding. Which one should look through json_last_error and correct accordingly. Most likely the problem is trivial with the encoding.

But in general, of course, generating Jason with his hands (!) In a SQL query (!!) is a tin of tin. It is not clear why you can not get the data and then encode them normally

  • You can of course of two implode () parse a two-dimensional array, I just wanted to make it easier for you to get by with just one json_decode :) - ruslan79k
  • @ ruslan79k so what json do you want to form in sql? what implode ()? got the data into the array and json_encode to help. - Naumov