dongwang788787 2019-07-24 09:22
浏览 110
已采纳

如何使用php在select选项中显示一些JSON数据?

I connect to an API and receive a JSON Data with PHP. After execute:

$countries= curl_exec($ch);
$jd_countries = json_decode($countries, TRUE);
print $countries;

The result of this is:

enter image description here

I would like to use for each to add the country_id and name to option value

So my code will be something like this:

<select name="country" class="form-control" >
    <?php foreach($jd_countries as $value){ ?>
        <option value="<?php echo $country_id;?>"><?php echo $name;?>
        </option> 
    <?php } ?>
</select>

I receive this:

Notice: Array to string conversion in C:\xampp\htdocs\account\index.php on line 28 Array

Notice: Array to string conversion in C:\xampp\htdocs\account\index.php on line 28 Array

Any suggestions? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duangou1551 2019-07-24 09:26
    关注
    <?php foreach($jd_countries['data'] as $country){ ?>
       <option value="<?php echo $country['country_id'];?>"><?php echo $country['name'];?>
       </option>
    <?php } ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?