2013-05-27 61 views
0

我有一個代碼,以貨幣輸出轉換爲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 amount標識,india.png as image

像期望的輸出

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

我能得到這個幫助。

感謝

+0

WUT? 「標識符」你是什麼意思?索引? – HamZa

+0

你已經試過了什麼?在這個函數中,你不會用$ amount做任何事情,例如... –

回答

3

,而不是

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

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

Thanls它的工作! – Yogus

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" 
     ) 
    );