2

I want to generate a selectbox using two arrays, one containing option-value and option-name and one containing data-value for the option

For example:

"arra1"  => array("1" => "orange", "2" => "banana", "3" => "apple"),
"data-array"  => array("first" , "second" , "third"),

and result must be

foreach( ??? ) {

<option value=1 data-value="first">orange</option>
<option value=2 data-value="second">banana</option>
<option value=3 data-value="third">apple</option>

}

Suggestions? Thanks

2
  • 2
    Do you have to use two arrays? The best suggestion I have is to use one. Commented Dec 19, 2016 at 10:59
  • use one array or a for loop in my opinion Commented Dec 19, 2016 at 11:04

6 Answers 6

3

Use PHP's array_values function to get both array with same indexing, then do the foreach:

$data = [
    "arra1"  => array("1" => "orange", "2" => "banana", "3" => "apple"),
    "data-array"  => array("first" , "second" , "third")
];

$labels = array_values($data["arra1"]);
$values = array_values($data["data-array"]);

foreach($labels as $index => $value) {
    $optionValue = $index+1;
    echo "<option value={$optionValue} data-value='{$values[$index]}'>{$labels[$index]}</option>";
}
Sign up to request clarification or add additional context in comments.

Comments

3

It actually uses one array ($key+1) is the key value of $array_1 if you are starting array key value from 1 not 0, This is a suggestion:

<?php

$arry_1  = array("1" => "orange", "2" => "banana", "3" => "apple");
$data_array  = array("first" , "second" , "third");

foreach ($data_array as $key => $value) {
  echo '<option value="'.($key+1).'" data-value="'.$value.'">orange</option>';
}
?>

1 Comment

You can use $arry_1 or $data_array single array only for foreach, If not strictly required two array
2

You could keep this more simplistic by doing the following:

<?php
foreach ($array as $index => $title)
{
    echo "<option data-value='" . $data[$index] . "'>$title</option>";
}
?>

Comments

1

i think you can achieve this using one associative array like below-

 //you can construct associative array like this
 $array = array("first"=>"orange","second"=>"banana","third"=>"apple");
 $count =1;
 foreach($array as $key=>$val)
 {
   echo '<option value='.$count.' data-value="'.$key.'"'.'>'.$val.'</option';
   $count++;  
 }

if you want to use two arrays as given in your question then-

$arr = ["arra1"  => array("1" => "orange", "2" => "banana", "3" =>   "apple"),
        "data-array"  => array("first" , "second" , "third"),];

for($i=1 ; $i<=count($arr['arra1']);$i++) 
{
  echo '<option value='.$i.'  data-value="'.$arr['data-array'][$i-1].'">'.$arr['arra1']["$i"].'</option>';
}

Comments

1

This can be helpful to get your desired output :

$arra1  = array("1" => "orange", "2" => "banana", "3" => "apple");
$data_array  = array("first" , "second" , "third");

echo "<select>";
foreach ($arra1 as $key => $value) {
  echo '<option value="'.($key).'" data-value="'.$data_array[$key-1].'">'.$value.'</option>';
}
echo "</select>";

Comments

1

Read about array_map. You can supply any number of arrays to it and traverse them kinda in parallel:

$options = array_map(function ($key, $value, $datum) {
    return "<option value=\"$key\" data-value=\"$datum\">$value</option>";
}, array_keys($arry_1), $arry_1, $data_array);

Here is working demo.

Pay attention that in order to pass keys along with values I have used array_keys function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.