0

我有一个功能

function currency($from_Currency,$to_Currency,$amount) {

成功地回显了结果。

echo currency("USD","INR",1.00),"india.png","\n";
echo currency("USD","EUR",1.00),"europe.png","\n";
echo currency("USD","GBP",1.00),"england.png","\n";
echo currency("USD","AED",1.00),"uae.png","\n";
echo currency("USD","CNY",1.00),"china.png","\n";
echo currency("USD","CAD",1.00),"canada.png","\n";
echo currency("USD","PKR",1.00),"pakistan.png","\n";
echo currency("USD","ZAR",1.00),"southafrica.png","\n";
echo currency("USD","CAD",1.00),"canada.png","\n";
echo currency("USD","JPY",1.00),"japan.png","\n";
echo currency("USD","NZD",1.00),"newzealand.png","\n";
echo currency("USD","NPR",1.00),"nepal.png","\n";

由于只有在函数中传递 USD、INR、EUR... 值时才能返回回声值。我还附加了图像名称。我想用回显值生成 JSON 输出。有人可以给我提供交流的想法吗?

我从回声值得到的输出是这样的

54.42india.png
0.77europe.png
0.65england.png
3.67uae.png
6.17china.png
1.02canada.png
98.48pakistan.png
9.1southafrica.png
1.02canada.png
98.22japan.png
1.18newzealand.png
87.01nepal.png

提前致谢

4

2 回答 2

3

据我了解您的问题最好将代码重构如下。

  • 创建一个包含您想要的所有数据的数组。
  • 然后使用json_enocde对数据进行编码 - 它返回一个值的 JSON 表示

例如:

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);

输出/输出

{"a":1,"b":2,"c":3,"d":4,"e":5}

然后使用json_decode对相关数据进行解码。

笔记

您不必使用回声来创建 JSON 对象。使用上述方法对您来说会容易得多。使用jsonlint验证您的 JSON 对象。

于 2013-04-28T07:09:14.183 回答
1

可能是创建标准类对象并将其转换为 json 的最简单方法。

$list = array();

$currency = new stdClass();
$currency->currency = currency("USD","INR",1.00);
$currency->image = "india.png";

$list[] = $currency;


echo json_encode($list);

如果你只想输出一个对象

echo json_encode($currency);
于 2013-04-28T07:13:24.353 回答