如何使用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:
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.
我连接到API并使用PHP接收JSON数据。 执行后: p>
$ countries = curl_exec($ ch);
$ jd_countries = json_decode($ countries,TRUE);
print $ countries;
code> pre>
结果如下: p>
我想使用每个将 country_id strong>和名称 strong>添加到选项值 p>
所以我的代码将是这样的 : p>
&lt; select name =“country”class =“form-control”&gt;
&lt;?php foreach($ jd_countries as $ value){?&gt;
&lt; option value =“&lt;?php echo $ country_id;?&gt;”&gt;&lt;?php echo $ name;?&gt;
&lt; / option&gt;
&lt;?php}?&gt;
&lt; / select&gt;
code> pre>
我收到了这个: p>
注意:第28行的C:\ xampp \ htdocs \ account \ index.php中的数组到字符串转换数组 p>
注意:C:\ xampp \ htdocs中的数组到字符串转换 第28行上的\ account \ index.php数组 p>
blockquote>
有任何建议吗? 提前致谢。 p>
div>
<?php foreach($jd_countries['data'] as $country){ ?>
<option value="<?php echo $country['country_id'];?>"><?php echo $country['name'];?>
</option>
<?php } ?>
You need to convert $countries to array
$jd_countries= json_decode($countries, TRUE);
<?php foreach($jd_countries->data as $country){ ?>
<option value="<?php echo $country['country_id'];?>"><?php echo $country['name'];?>
</option>
<?php } ?>