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条)

报告相同问题?

悬赏问题

  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)
  • ¥15 交叉注意力机制的残差问题
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?
  • ¥15 关于Java对接海康威视车牌识别一体机SDK是否需要固定外网的IP?
  • ¥15 Linux扩容时,格式化卡住了:vgdispaly查看卷组信息,没有输出