如何将数据从 JSON 检索到组合框


How to retrieve datas from JSON to a combo box?

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

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

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

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

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

然后只做每个

$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>