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;
Related
I have a select box and wanting to get the key of the next value in array to go with the option here is my code
<select>
<?php foreach ($make as $key => $make):?>
<option value="<?php echo next($key);//not correct ?> - <?php echo $key; ?>"> <?php echo $make; ?></option>
<?php endforeach;
Here is the array
Array
(
[0] => Brand
[1] => Alfa Romeo
[123] => Alpina
[142] => Aston Martin
[152] => Audi
[619] => Bentley
[640] => BMW
[1122] => Buick
)
This will work with an associative array as well as numerically indexed:
<?php foreach ($make as $key => $value):?>
<?php next($make); ?>
<option value="<?php echo key($make); ?> - <?php echo $key; ?>"> <?php echo $value; ?></option>
<?php endforeach; ?>
Note, it's confusing and probably a bad idea to use the same variable name for iterating as the array. so instead of foreach ($make as $key => $make) I did foreach ($make as $key => $value) here.
The code above simply advances the pointer on the array and then gets the key for your option value using key(). Since there is no next key on the final array element, the value will be empty.
You can resolve your issue with a lot of solution, but if you have an assoiatve array or none order array or not numirc, you can use this:
<?php
$array = ["a" => 1, "b" => 2, "c" => 3, 1 => "aa"];
next($array);
$key = key($array);
echo $key;
prev($array);
$key = key($array);
echo $key;
Else, if array is ordered and numeric you can use $key + 1;
Simple way to get key of next element is to use array_search of next element of array e.g.
<?php foreach ($makers as $key => $make):?>
<option value="<?php echo array_search( next($makers), $makers );"> <?php echo $make; ?></option>
<?php endforeach;
Hope this helps.
try this function. it finds current key of given value in the array, and from that found key's position, you can get next/previous key with $increment
Ex: when $increment=1, it finds next key
when $increment=2, it finds 2 next key
when $increment=-1, it finds 1 previous key, and so on.
function sov_find_key($findvalue, $array, $increment) {
reset($array);
$key = array_search($findvalue, $array);
if ($key === false || $key === 0){
return false;
}
if ($increment === 0){
return $key;
}
$isNegative = $increment < 0 ? true:false;
$increment = abs($increment);
while(key($array) !== $key) {
next($array);
}
$x=0;
while($x < $increment) {
if( $isNegative ){
prev($array);
} else {
next($array);
}
$x++;
}
return key($array);
}
DEMO: https://3v4l.org/CSSmR
<select>
<?php
foreach ($make as $key => $make):
?>
<option value="<?php echo $key + 1; ?> - <?php echo $key; ?>">
<?php echo $make;
?>
</option>
<?php
endforeach;
Just increment the $key by 1, and you will have the next array element if the keys are in order and are numeric.
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>';
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 a big array that is being used throughout the site and changing its structure just for the following task would be a pain. I want to make several select boxes with another array. The output I want to get is like this:
/* Example Array:
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals",
"Crocodilians","Turtles");
$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
*/
<select name='Amphibian'>
<option value='0'>Frogs</option>
<option value='1'>Toads</option>
</select>
<select name='Mammal'>
<option value='2'>Bats</option>
<option value='3'>Elephants</option>
<option value='4'>Rats</option>
<option value='5'>Seals</option>
</select>
<select name='Reptile'>
<option value='6'>Crocodilians</option>
<option value='7'>Turtles</option>
</select>
I can't figure out how to print out the options only when the value is within the specific animal group during each iteration of $group. I have tried each() to get the next animal $endAnimal from $group and then break the internal loop if $animal matches the $endAnimal, but I also need to make the loop start printing options at specific values.
<?php
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");
$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
foreach($group as $thisAnimal=>$category){
$nextKey = (each($group));
$endAnimal = $nextKey['key'];
print "<select name='$category'>";
foreach($allAnimals as $idx=>$animal){
print "<option value='$idx'>$animal</option>";
if($endAnimal === $animal){
break;
}
}
print "</select>";
}
?>
Please check this out:-
<?php
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");
$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
$group_keys = array_keys($group); // get the keys of group array
$j = 0;
foreach($group_keys as $key => $group_k){
print "<select name='$group[$group_k]'>";
if(isset($group_keys[$key+1])){
$new_value = $group_keys[$key+1];
}else{
$new_value = '';
}
if($new_value ==''){
foreach($allAnimals as $key => $allAnm){
print "<option value='$j'>$allAnm</option>";
unset($allAnimals[$key]);
$j ++;
}
}else{
$key_from = array_search($new_value,$allAnimals);
for($i = 0; $i<$key_from; $i++){
print "<option value='$j'>$allAnimals[$i]</option>";
unset($allAnimals[$i]);
$j ++;
}
}
$allAnimals = array_values($allAnimals);
print "</select>";
}
Output:- https://eval.in/379462 (eval.in)
local end :- (for better showing) :- http://prntscr.com/7fmtbi
Array of Array is the solution I think
$group = array( "Amphibian" => array("Frogs", "Toads"), "Mammal" => array("Bats", "Elephants", "Rats", "Seals"), "Reptile" => array("Crocodilians", "Turtles") );
foreach($group as $thisAnimal=>$category){
print "<select name='$category'>";
foreach($category as $idx=>$animal){
print "<option value='$idx'>$animal</option>";
}
print "</select>";
}
Thank you for your responses and answers. I just sort of get that to work by checking whether $thisAnimal from $group is the same as $animal from $allAnimals during the internal loop. If it's the same, turn $passThis to true and then proceed to print out the option until $endAnimal matches $animal. Example
<?php
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");
$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
foreach($group as $thisAnimal=>$category){
$nextKey = (each($group));
$passThis = false;
$endAnimal = $nextKey['key'];
print "<select name='$category'>";
foreach($allAnimals as $idx=>$animal){
if($animal === $thisAnimal){
$passThis = true;
}
if($endAnimal === $animal){
break;
}
if($passThis === true)
{
print "<option value='$idx'>$animal</option>";
}
}
print "</select>";
}
?>
In PHP, I am having 2 array
$ex1 = array(a,b,c,d,e,f,g,h);
$ex2 = array(c,e,f);
Here how can I integrate this with multiple select option in PHP page
Here ex1 is the multiple select array like
<select multiple name=slt[]>
</select>
And ex2 values are the chosen listing options
Something like:
<?php
$ex1 = array('a','b','c','d','e','f','g','h');
$ex2 = array('c','e','f');
echo "<select multiple name=slt[]>";
foreach($ex1 as $val){
//in_array() checks if value from 1st array ($val) is present
//anywhere in the second array ($ex2)
//if yes, that option will be selected. I'm using ternary operator
//here instead of if statement
$selected = (in_array($val,$ex2))?' selected':'';
echo "<option value='".$val."'$selected>".$val."</option>";
}
echo "</select>";
?>
PHP Demo
Output Fiddle
I'm not sure about what you want to do, but here's an idea :
$ex1 = array("a","b","c","d","e","f","g","h");
echo "<select multiple>";
foreach( $ex1 as $value ){
echo "<option value='$value'>$value</option>";
}
echo "</select>";
Try running it:
<?php
$ex1 = array('a','b','c','d','e','f','g','h');
$ex2 = array('c','e','f');
?>
<select multiple name=slt[]>
<?php
foreach ($ex1 as $option) {
$active = false;
foreach($ex2 as $selected){
if($option == $selected){
$active = true;
}
}
?>
<option value="<?php echo $option; ?>" <?php if($active===true) echo "selected"; ?>><?php echo $option; ?></option>
<?php
}
?>
</select>