1

下面的代码是 json(a part only) ,我必须将整个货币放入一个组合框中。

{
    "AED": "United Arab Emirates Dirham",
    "AFN": "Afghan Afghani",
    "ALL": "Albanian Lek",
    "AMD": "Armenian Dram",
    "ANG": "Netherlands Antillean Guilder",
    "AOA": "Angolan Kwanza",
    "ARS": "Argentine Peso"
}

我已经尝试到这里...

$ch2 = curl_init("http://openexchangerates.org/api/currencies.json");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);

// Get the data:
$json2 = curl_exec($ch2);
curl_close($ch2);

$currency->LKR; // Here I can retreive a single line

// Decode JSON response:
$currency = json_decode($json2);

我必须将整个货币放入组合框中。

4

1 回答 1

3

假设您的 $currency 数组看起来像

$currency = array('AED' => 'United Arab Emirates Dirham' ...... );

//然后只做foreach

$opts = '';
foreach($currency as $key => $val)
{

$opts .= '<option value="'.$key.'">'.$val.'</option>';
}

echo  '<select name="currency">'.$opts.'</select>;

这应该输出类似...

<select name="currency">
<option value="AED">United Arab Emirates Dirham</option>
<option value="AFN">Afghan Afghani</option>

.....

</select>
于 2012-08-16T11:15:23.810 回答