I have the following array:
$selects = array(
'Select1' => array('select1_name' => array('select1_value1','select1_value1')),
'Select2' => array('select2_name' => array('select2_value1','select2_value2'))
);
I wonder how I can generate these "selects inputs" with their options through a loop?
You need one cycle, which will loop through selects array and inside this cycle, you need another one, which will loop through selects. And inside this one, you need one more, which will loop through the option values:
$selects = array(
'Select1' => array('select1_name' => array('select1_value1','select1_value1')),
'Select2' => array('select2_name' => array('select2_value1','select2_value2'))
);
foreach($selects as $select) {
foreach($select as $item) {
echo "<select>";
foreach($item as $value) {
echo "<option value=".$value.">".$value."</option>";
}
echo "</select>";
}
}
This will produce:
<select>
<option value=select1_value1>select1_value1</option>
<option value=select1_value1>select1_value1</option>
</select>
<select>
<option value=select2_value1>select2_value1</option>
<option value=select2_value2>select2_value2</option>
</select>
foreach($selects as $select) {
foreach($select as $selectName => $value) {
echo '<select> ';
echo '<option>'.$selectName.'</option>';
foreach($value as $v) {
echo '<option>'.$v.'</option>';
}
echo '</select>';
}
}
echo '<select> ';
foreach($selects as $array) {
foreach($array as $value) {
foreach($value as $v) {
echo '<option value="'.$v.'">'.$v.'</option>';
}}}
echo '</select>';
Related
I have this kind of array shown in Picture. How can i insert the values in the select option as
<option value="7">7</option>
<option value="13000">13000</option>
<option value="19AAAAA">19AAAAA</option>
<option value="sdsdas">sdsdas</option>
<option value="dasdasdasd">dasdasdasd</option>
Simply flatten the multi-dimensional array, and then loop through it.
Suppose $arr in your original multi-dimensional array
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach($it as $value){
?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php
}
Here are the relevant references:
http://php.net/manual/en/class.recursiveiteratoriterator.php
http://php.net/manual/en/class.recursivearrayiterator.php
<?php
foreach($arr as $val){
foreach($val as $val2){
foreach($val2 as $val3){ ?>
<option value="<?php echo $val3;?>"><?php echo $val3 ;?></option><?php
}
}
}
?>
You need to flat array. You can do it with recursive function. Here you have general function for that.
/**
* Get multilevel array convert to single-level array
* #param $array
* #return array
*/
function getFlattened($array) {
$flattened = [];
foreach ($array as $flat) {
if (is_array($flat)) {
$flatArray = array_merge($flatArray, getFlattened($flat));
} else {
$flattened[] = $flat;
}
}
return $flattened;
}
Of course you can use that approach to recursively display select - not only to flat array.
<?php foreach($array as $inner): ?>
<?php foreach($inner as $innerTwo): ?>
<?php foreach($innerTwo as $item): ?>
<option value="<?= $item ?>"><?= $item ?></option>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
you may try this.
<?php
$input = Array(
Array
(
0 => 7,
1 => 13000
),
Array
(
0 => '19AAAAA',
1 => 'sdsdas'
)
);
$options = "";
$result = call_user_func_array("array_merge", $input);
for($i = 0;$i< count($result);$i++ ){
$options .="<option value='".$result[$i]."'>".$result[$i]."</option>";
}
echo $options;
I have this UI:
Also, I have this PHP Back-end code:
<select name="as<?php echo $product['product_id']; ?>[]" style="width:250px;">
<?php
foreach ($product['uniSku'] as $key => $value) {
echo '<option value="'.$key.'">'.$value.'</option>';
}
?>
</select>
The question is, how can i put the data SKU from array into the select box?
You need to iterate twice in your multidimesional array
foreach ($product['uniSku'] as $data) {
foreach($data as $key => $val) {
echo '<option value="'.$key.'">'.$val.'</option>';
}
}
Or if you need to use keys from your parent array you can store it in a variable at first iteration
foreach ($product['uniSku'] as $kk => $data) {
foreach($data as $key => $val) {
echo '<option value="'.$kk.'">'.$val.'</option>';
}
}
Try this
Replace values with $value['sku']
foreach ($product['uniSku'] as $key => $value) {
echo '<option value="'.$key.'">'.$value['sku'].'</option>';
}
Here the solution
foreach ($product['uniSku'] as $key => $value) {
echo '<option value="'.$key.'">'.$value['sku'].'</option>';
}
How can I show only name and lastname for mecanicien?
$aMecaniciens = array(
array(
"idMecanicien"=>1,
'vchNomMecanicien'=>"Guérand",
'vchPrenomMecanicien'=>"Bob"
),
array(
"idMecanicien"=>2,
'vchNomMecanicien'=>"Lim",
'vchPrenomMecanicien'=>"Bao"
),
array(
"idMecanicien"=>3,
'vchNomMecanicien'=>"Cadoret",
'vchPrenomMecanicien'=>"Cadoret"
)
);
foreach ($aMecaniciens as $value) {
foreach ($value as $key) {
echo "<option value=\"value\">$key[1].$key[2]</option>";
}
}
You don't need the inner foreach. You can use the named keys of $value to output your options.
foreach ($aMecaniciens as $value) {
echo "<option value=\"$value[idMecanicien]\">
$value[vchNomMecanicien].$value[vchPrenomMecanicien]
</option>";
}
With the nested loop you will output three options for each item in $aMecaniciens, which I assume you don't want.
foreach($aMecaniciens as $key => $value){
echo '<option value="'.$value['idMecanicien'].'">'.$value['vchNomMecanicien'].' '.$value['vchPrenomMecanicien'].'</option>';
}
I am comparing three arrays in nested foreach conditions. Following are the arrays
Array
(
[master/city] => City
[master/national_holiday] => National Holiday
[master/operator_comments] => Operator Comments
[master/sensors] => Sensors
[master/modbus] => Modbus
[master/manufacturers] => Manufacturers
[master/make_model] => Make Model
[master/dispatch_vendors] => Dispatch Vendors
)
Array
(
[1] => View
[2] => Write
)
Array
(
[master/city] => 1
[master/national_holiday] => 2
[master/operator_comments] => 1
[master/sensors] => 2
[master/modbus] => 1
[master/manufacturers] => 2
[master/make_model] => 1
)
Now the scenario is as follows:-
My first foreach iteartes first array
Then in the same foreach i m using second foreach which itrates second array
again in second foreach i m using third foreach to iterate third array
In third foreach , i m comparing key of first array with the key of second array and comparing value of second array with key of third array
If above condition is satisfied then in my dropdown the specific option will append selected Like <option value="1" selected="">View</option>
I am using following code
<?php
$first_array = first_array();
$i = 1;
foreach($first_array as $k => $val) {
?>
<tr>
<td>{{ $i }}</td>
<td class="mailbox-name">{{ $val }}</td>
<td><?php $second_array = second_array(); ?>
<select class="form-control master-menu" name="master_menu[{{$k}}]">
<option value="">Select Role</option>
<?php
foreach ($second_array as $key => $value) {
foreach ($third_array as $mkey => $mval) {
?>
<option value="<?php echo $key; ?>"
<?php if (($mkey == $k) && ($mval == $key)) { echo "selected"; } ?>><?php echo $value; ?></option>
<?php } } ?>
</select>
</td>
</tr>
<?php $i++; } ?>
I am using above code and getting issue that in second array there two values and in third array five values so in my dropdown count of option are ten insted of two.
This is my output.
Please suggest me.
Maybe something like this? I have simplified the process to demonstrate what is happening. I have also added the correct select values:
foreach ($first_array as $key => $value) {
?>
<p><?php echo $value; ?></p>
<?php foreach ($second_array as $second_key => $second_value) { ?>
<?php if ($key == $second_key) { ?>
<select>
<?php foreach ($third_array as $third_key => $third_value) { ?>
<option <?php echo ($third_key == $second_value ? 'selected=selected' : null); ?>><?php echo $third_value; ?></option>
<?php } ?>
</select>
<?php } else { ?>
<select>
<?php foreach ($third_array as $third_key => $third_value) { ?>
<option ><?php echo $third_value; ?></option>
<?php } ?>
</select>
<?php } ?>
<?php } ?>
<?php
}
For example you may try this code
foreach ($tmparray as $innerarray) {
//check type
if (is_array($innerarray)) {
//echo through inner loop
foreach ($innerarray as $value) {
echo $value;
}
} else {
//one,two,three
echo $innerarray;
}
}
I have an array that looks something like this:
$array = array(
'val1' => array('en'=>'Option Title 1','a'=>1),
'val2' => array('en'=>'Option Title 2','b'=>2),
'val3' => array('en'=>'Option Title 3','c'=>3)
);
I tried running this code to generate a select box :
function setOptions($array){
echo '<select name="select">';
foreach($array as $key_parent => $val_parent){
foreach($val_parent as $key => $val){
if($key !== 'en'){
$option_value = $val;
}elseif($key == 'en'){
$option_title = $val;
}
}
echo '<option value"'.$option_value.'">'.$option_title.'</option>';
}
echo '<select>';
}
This prints what I expected.
<select name="select">
<option value="1">Option Title 1</option>
<option value="2">Option Title 2</option>
<option value="3">Option Title 3</option>
</select>
However when I print the return value of $_POST['select'] from a form submission, I get Option Title 1, Option Title 2, or Option Title 3, not 1,2 or 3 which I thought was pretty odd.
So this leaves the question, how do I get the values inside of val1, val2, val3 when the key of one of the values in question is unknown(either a,b or c)?
You miss equal in value attribute.
function setOptions($array){
echo '<select name="select">';
foreach($array as $key_parent => $val_parent){
foreach($val_parent as $key => $val){
if($key !== 'en'){
$option_value = $val;
}elseif($key == 'en'){
$option_title = $val;
}
}
echo '<option value="'.$option_value.'">'.$option_title.'</option>';
}
echo '<select>';
}