How to get a values from array and display as select list option

In the database for WordPress in one of the plugins the values where entered as array into the fields from the admin. Now I want to get the values from that array and display in the page.

I created a function, This works fine.

Read More
// Pull district from database
function get_district(){
    global $wpdb;
    $current_demand = get_metadata('post', 205, 'field_52a7cb0d1edb0');
    return $current_demand;
}

On the page where I want to use the array values I did the var_dump to check for the results. This also works great

var_dump(get_district());

The values returns from the results where in this format. From what I can say is that its a array.

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

So What I did next was to break it down and look for the specific values from the array. Now I’ am running this code on the function

    foreach(get_district() as $district => $key){
        $test = $key["choices"];
        var_dump($test);
    }

The results returned from above code was

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" 
}

Here’s my question, How can I now get the values from here to a select list. This are correct values that I want to use inside the select list. This is the first time am seeing something like this.

Related posts

Leave a Reply

2 comments

  1. Try this one

    $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  */
    }
    

    In html

    <select name="districts">
    <?php echo $options; ?>
    </select>
    
  2. A foreach loop is your best bet.

    <?php
        $selectData = get_district();
        $selectData = $selectData[0]['choices'];
    
        $select = "<select>";
        foreach($selectData as $key => $value){
            $select .= "<option value='$value'>$key</option>";
        }
        $select .= "</select>";