With fetchAll(PDO::FETCH_ASSOC) get such array
$array =
Array
(
[0] => Array
(
[FinalCurrencyRate] => 0.000062
)
)
Need to json_encode to use in
$.post("__02.php", { 'currency': currency }, function(data, success) {
$('#currency_load').html(data.FinalCurrencyRate);
$('#currency_load0').html(data.FinalCurrencyRate0);
$('#currency_load1').html(data.FinalCurrencyRate1);//and so on
}, "json");
If simply echo json_encode($array); then does not work.
Need to convert to json format array like this:
$arr = array ('FinalCurrencyRate'=>"0.000062");
To convert to json format, wrote such code
$json_array = "{";
$flag_comma = 0;
foreach($array as $i =>$array_modified) {
if ($flag_comma == 0) {
$json_array .="'FinalCurrencyRate". $i."'=>'". $array_modified[FinalCurrencyRate]. "'";
$flag_comma = 1;
}
else {
$json_array .=", 'FinalCurrencyRate". $i."'=>'". $array_modified[FinalCurrencyRate]. "'";
}
}
$json_array .= "}";
Then echo json_encode($json_array);. And only one echo json_encode.
But does not work.
Understand that there are errors in code to convert to json format. What need to correct, please?
json_encodeis perfectly able to turn a multidimensional array into a json string.