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

时间:2012-08-16 11:11:36

标签: php json

下面的代码是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);

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

1 个答案:

答案 0 :(得分:3)

假设您的$货币数组看起来像

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