Php 来自JSON链接的YII2 Dropdownlist
如何将json数据填充到dropdownlist 这是json链接: 这是json格式:Php 来自JSON链接的YII2 Dropdownlist,php,json,yii2,active-form,Php,Json,Yii2,Active Form,如何将json数据填充到dropdownlist 这是json链接: 这是json格式: [{"inst_id":"1","inst_code":"001","inst_name":"HARVARD"},{"inst_id":"2","inst_code":"002","inst_name":"UCLA"}] 这一观点: <?= $form->field($model, 'institusi')->dropDownList(); ?> “inst_id”的下拉值和
[{"inst_id":"1","inst_code":"001","inst_name":"HARVARD"},{"inst_id":"2","inst_code":"002","inst_name":"UCLA"}]
这一观点:
<?= $form->field($model, 'institusi')->dropDownList(); ?>
“inst_id”的下拉值和“inst_name”的文本
我有代码,但它不是活动窗体
$url = 'http://localhost/data';
$content = file_get_contents($url);
$json = json_decode($content, true);
$inst=array();
echo "<select>";
foreach($json as $item) {
echo "<option value='".$item['inst_id']."'>".$item['inst_name']."</option>";
}
echo "</select>";
$url='1!'http://localhost/data';
$content=file\u get\u contents($url);
$json=json_decode($content,true);
$inst=array();
回声“;
foreach($json作为$item){
回显“$item['inst_name']”;
}
回声“;
因此,如何将json数据填充到Activeform Dropdownlist中,最简单的方法是在它之前为Dropdownlist准备数组
<?php
$url = 'http://localhost/data';
$content = file_get_contents($url);
$json = json_decode($content, true);
$instArray = ArrayHelper::map($json,'inst_id','inst_name');
echo $form->field($model, 'institusi')->dropDownList($instArray);
?>
您还可以从控制器准备相同的数组,并通过render
方法将其传递给view
其他选项是创建组件或向模型添加静态方法。两者都应该返回json数据的数组格式