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

FireFoxII
  • 828
  • 4
  • 18
  • 31

6 Answers6

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>';
}
?>
LF00
  • 27,015
  • 29
  • 156
  • 295
Amit Kumar Sahu
  • 495
  • 6
  • 15
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>";
}
LF00
  • 27,015
  • 29
  • 156
  • 295
Elijah
  • 46
  • 1
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>";
}
?>
Option
  • 2,605
  • 2
  • 19
  • 29
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>';
}
Veshraj Joshi
  • 3,544
  • 3
  • 27
  • 45
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>";
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.

sevavietl
  • 3,762
  • 1
  • 14
  • 21