如何为此JSON输出提供标识符

时间:2013-05-27 08:16:31

标签: php arrays json

我有一个代码将货币输出转换为JSON响应

<?php
$response = array();
function currency($from_Currency, $to_Currency, $amount)
{
    $amount        = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency   = urlencode($to_Currency);
    $url           = "Myurl";
    $ch            = curl_init();
    $timeout       = 0;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    $data = explode('"', $rawdata);
    $data = explode(' ', $data['3']);
    $var  = $data['0'];
    return round($var, 2);
}

$result = array(
    array(
        currency("USD", "INR", 1.00),
        "india.png"
    ),
    array(
        currency("USD", "EUR", 1.00),
        "europe.png"
    ),
    array(
        currency("USD", "JPY", 1.00),
        "japan.png"
    )
);
echo json_encode(array('item' => $result));
?>

输出

{"item":[[55.81,"india.png"],[0.77,"europe.png"],[101.07,"japan.png"]]}

我想提供55.81 as amountindia.png as image等标识符。

喜欢所需的输出

{"item":[{"amount":"55.7","image"="india.png"},{"amount"="0.77","image"="europe.png"},{"amount"="101.07","image"="japan.png"}]}

我能否得到这方面的帮助。

由于

2 个答案:

答案 0 :(得分:3)

而不是

array(
    currency("USD", "INR", 1.00),
    "india.png"
)

array(
    "amount" => currency("USD", "INR", 1.00),
    "image" => "india.png"
)

答案 1 :(得分:1)

您可以尝试更改:

$result = array(
        array(
            'amount' => currency("USD", "INR", 1.00),
            'image' => "india.png"
        ),
        array(
            'amount' => currency("USD", "EUR", 1.00),
            'image' => "europe.png"
        ),
        array(
            'amount' => currency("USD", "JPY", 1.00),
            'image' => "japan.png"
        )
    );