Set select box value from array - php

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>';
}

Related

Foreach array inside array

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>';
}

Generate "select inputs" through loops in arrays

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>';

How to get the $key from a multidimentional array using php?

Below is a simple array that I created:
$colors = array(
"parent1" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
),
"parent2" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
)
);
What I need to get is the key of my level 1 arrays which are string "parent1" and "parent2".
Currently I'm using foreach with while loop to get the key
foreach ($colors as $valuep) {
while (list($key, $value) = each($colors)) {
echo "$key<br />";
}
}
but I'm only able to get the "parent2" string from using the above method and not "parent1".
You're so close.
foreah($colors as $key => $val)
{
echo $key . "<br/>";
}
Use the key like so:
foreach ($colors as $key => $value) {
echo $key.'<br>';
}
To print out the keys:
foreach ($colors as $key => $value) {
echo $key . '<br />';
}
You can also get all of the keys from an array by using the array_keys() method, for example:
$keys = array_keys($colors);
foreach ($keys as $key) {
echo $key . '<br />';
}

Adding a value to a PHP associative array

I'm passing an array inside a GET(url) call like this:
&item[][element]=value
Then I do my stuff in PHP:
$item = $_GET['item'];
foreach ($item as $aValue) {
foreach ($aValue as $key => $value) {
echo '$key $value';
The problem I'm facing is that I need to have(echo) a third 'value':
echo '$key $value $thirdValue';
Do I have to change my the URL I'm passing or the foreach? And how do I do that? I Googled but I can't make heads nor tails out of it.
$item = $_GET['item'];
$item_temp=array_values($item);
foreach ($item as $aValue) {
foreach ($aValue as $key => $value) {
echo '$key $value'.$item_temp[2];
}
}
<?php
$item = $_GET['item'];
$r=array();
foreach($item as $rt){
array_push($r,array(key($rt)=> $rt));
}
foreach($r as $rt){
foreach($rt as $rt2){
$k = key($rt2);
echo $k.'__'.$rt2[$k] ;
echo "<br>";
}
}
?>
it's Work .

PHP - Can a foreach be used on an array that contains strings?

I have an array of strings and I am trying to implement a foreach to echo each of the strings like the following:
$options = array('string1', 'string2', 'string3', 'string4', 'string5');
foreach ($options as $option)
{
echo $option;
}
I've also tried
foreach (options as $key => $option)
Any help is appreciated!
Of course you can - see other answers. I just want to add this consideration:
$options = array('string1', 'string2', 'string3', 'string4', 'string5');
foreach ($options as $option) // Prints 'string1', 'string2' and so on
{
echo $option;
}
If you also need the index (variabile $i will be "binded" to the index, starting from zero):
foreach ($options as $i => $option) // Prints '0', '1' and so on (as numbers)
{
echo $i;
}
Missing $:
foreach ($options as $option)
{
echo $option;
}
Oh yes you can very well use it.
<?php
$options = array('string1', 'string2', 'string3', 'string4',
'string5');
foreach ($options as $valueinoptions)
{
echo $valueinoptions;
echo "<br>";
}
?>

Categories