I have done the same thing several times (also on the same project actually) and it works fine. I have an issue just with this instance of the same code. First of all I select some data from a db table:
$azioni = $pdo->query("SELECT id_az, concat_ws('-',Descrizione, RGE) as descrizione
FROM azioni_head
JOIN sofferenze ON sofferenze.id_soff = azioni_head.id_soff
ORDER BY Descrizione")
->fetchAll(PDO::FETCH_ASSOC);
This gives me an array like this (just first few items):
Array
(
[0] => Array
(
[id_az] => AZ000000126
[descrizione] => Acciaierie Weissenfels S.p.A.-n/d
)
[1] => Array
(
[id_az] => AZ000000017
[descrizione] => Acofer S.p.A.-n/d
)
)
Then I convert this array in a Json array doing:
var azioni = <?php echo json_encode($azioni); ?>;
and finally I populate a Select2 using these data but this time the select2 has no items inside.
If I try to view the array once it is encoded using alert(azioni.join( )); I get:
[object Object],[object Object],[object Object],[object Object],
Finally I run:
$('#cod_az').select2({ placeholder: "Scegli", data: azioni });
What is wrong? Why I cannot use this array to populate Select2 with the data? It works in other section of my application!
Edit this is the result of console.log(azioni) in firebug:
[
Object { id_az="AZ000000126", descrizione="Acciaierie Weissenfels S.p.A.-n/d"},
Object { id_az="AZ000000017", descrizione="Acofer S.p.A.-n/d"},
Object { id_az="AZ000000039", descrizione="ADANI SAS DI ADANI PAOLO & C. S.p.A.-n/d"},
Object { id_az="AZ000000019", descrizione="Administration Speciale ...NG S.A. en faillite-n/d"}
]
console.log(azioni)and look at your Javascript console.alert()is useless for debugging.