Reputation: 195
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.
Upvotes: 0
Views: 787
Reputation: 410
<?php foreach($jd_countries['data'] as $country){ ?>
<option value="<?php echo $country['country_id'];?>"><?php echo $country['name'];?>
</option>
<?php } ?>
Upvotes: 3
Reputation: 665
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 } ?>
Upvotes: 0