Php 如何从数组中获取值并显示为选择列表选项
在其中一个插件的WordPress数据库中,将作为数组输入的值从管理员输入字段。现在我想从该数组中获取值并显示在页面中 我创建了一个函数,效果很好Php 如何从数组中获取值并显示为选择列表选项,php,arrays,wordpress,Php,Arrays,Wordpress,在其中一个插件的WordPress数据库中,将作为数组输入的值从管理员输入字段。现在我想从该数组中获取值并显示在页面中 我创建了一个函数,效果很好 // Pull district from database function get_district(){ global $wpdb; $current_demand = get_metadata('post', 205, 'field_52a7cb0d1edb0'); return $current_demand; }
// Pull district from database
function get_district(){
global $wpdb;
$current_demand = get_metadata('post', 205, 'field_52a7cb0d1edb0');
return $current_demand;
}
在我想使用数组值的页面上,我进行了var_转储以检查结果。这也很有效
var_dump(get_district());
这些值以这种格式从结果返回。我能说的是它是一个数组
array(1) {
[0]=> array(12) {
["key"]=> string(19) "field_52a7cb0d1edb0"
["label"]=> string(8) "District"
["name"]=> string(8) "district"
["type"]=> string(6) "select"
["instructions"]=> string(0) ""
["required"]=> string(1) "0"
["choices"]=> array(10) {
["suva"]=> string(4) "Suva"
["nausori"]=> string(7) "Nausori"
["nadi"]=> string(4) "Nadi"
["lautoka"]=> string(7) "Lautoka"
["ba"]=> string(2) "Ba"
["tavua"]=> string(5) "Tavua"
["rakiraki"]=> string(8) "RakiRaki"
["tailevu"]=> string(7) "Tailevu"
["navua"]=> string(5) "Navua"
["labasa"]=> string(6) "Labasa"
}
["default_value"]=> string(0) ""
["allow_null"]=> string(1) "0"
["multiple"]=> string(1) "0"
["conditional_logic"]=> array(3) {
["status"]=> string(1) "0"
["rules"]=> array(1) {
[0]=> array(2) {
["field"]=> string(4) "null"
["operator"]=> string(2) "=="
}
}
["allorany"]=> string(3) "all"
}
["order_no"]=> int(1)
}
}
所以我接下来要做的就是分解它,从数组中寻找特定的值。现在我正在函数上运行这段代码
foreach(get_district() as $district => $key){
$test = $key["choices"];
var_dump($test);
}
从上述代码返回的结果为
array(10) {
["suva"]=> string(4) "Suva"
["nausori"]=> string(7) "Nausori"
["nadi"]=> string(4) "Nadi"
["lautoka"]=> string(7) "Lautoka"
["ba"]=> string(2) "Ba"
["tavua"]=> string(5) "Tavua"
["rakiraki"]=> string(8) "RakiRaki"
["tailevu"]=> string(7) "Tailevu"
["navua"]=> string(5) "Navua"
["labasa"]=> string(6) "Labasa"
}
这里是我的问题,我现在如何才能从这里获得选择列表中的值。这是我想在选择列表中使用的正确值。这是我第一次看到这样的东西。试试这个
$districts =get_district(); /* get all data */
$options="";
/* loop through all the choices array */
foreach($districts[0]['choices'] as $key => $val){
$options.="<option value='".$key."'>".$val."</option>";
/* append the options of select in $options */
}
在html中
<select name="districts">
<?php echo $options; ?>
</select>
试试这个
$districts =get_district(); /* get all data */
$options="";
/* loop through all the choices array */
foreach($districts[0]['choices'] as $key => $val){
$options.="<option value='".$key."'>".$val."</option>";
/* append the options of select in $options */
}
在html中
<select name="districts">
<?php echo $options; ?>
</select>
循环是你最好的选择
<?php
$selectData = get_district();
$selectData = $selectData[0]['choices'];
$select = "<select>";
foreach($selectData as $key => $value){
$select .= "<option value='$value'>$key</option>";
}
$select .= "</select>";
循环是你最好的选择
<?php
$selectData = get_district();
$selectData = $selectData[0]['choices'];
$select = "<select>";
foreach($selectData as $key => $value){
$select .= "<option value='$value'>$key</option>";
}
$select .= "</select>";
抱歉,此代码给了我警告:为foreach提供的参数无效in@navneil-naicker我引用了错误的数组元素。我已经更新了代码$selectData=$selectData[0]['choices'];抱歉,此代码给了我警告:为foreach提供的参数无效in@navneil-naicker我引用了错误的数组元素。我已经更新了代码$selectData=$selectData[0]['choices'];