Gafwebmaster
Gafwebmaster

Reputation: 41

PHP: add array value into <option>

I am looking for a way to create

 <option>.Name1.</option>
<option>.OtherName.</option>

so on for every top value, reason to think how to extract the array value, without to know the name (Name1, OtherName, etc, that values will come dynamic). For print_r or var_dump you will see the below structure:

array (
  'Name1' => 
  array (
    'diametru' => 
    array (
      0 => 15,
      7 => 16,
    ),
    'latime' => 
    array (
      0 => 6,
      9 => 5,
    ),   
  ),
  'OtherName' => 
  array (
    'diametru' => 
    array (
      0 => 16,
      2 => 17     
    ),
    'latime' => 
    array (
      0 => 6,
      1 => 7,
      10 => 5,
      35 => 8,
    ),   
  ),
.........
)

I will be grateful for any help you can provide.

Upvotes: 1

Views: 684

Answers (3)

Vamsi
Vamsi

Reputation: 421

Use array_keys

$array = [
    'Name1' => [
        'diametru' => [7,6]
    ],
    'othername' => []
];
$output = array_keys($array);
$option = '';
foreach ($output as $val) {
    $option .= "<option>$val</option>";
}
print_r($option);

Upvotes: 0

Yanis-git
Yanis-git

Reputation: 7875

you have many way to do it :

$var = [
  'Name1' => [
    'diametru' => [
      0 => 15,
      7 => 16,
    ],
    'latime' => [
      0 => 6,
      9 => 5,
    ],   
  ],
  'OtherName' => [
    'diametru' => [
      0 => 16,
      2 => 17     
    ],
    'latime' => [
      0 => 6,
      1 => 7,
      10 => 5,
      35 => 8,
    ],   
  ]
];

foreach ($var as $index => $value) {
    echo "<option>$index</option>";
}

foreach (array_keys($var) as $index) {
    echo "<option>$index</option>";
}

Upvotes: 2

HamzaNig
HamzaNig

Reputation: 1029

you can just use Foearch :

<select>
<?php 
 foreach($array as $keyname=> $list)
{

echo '<option value="'.$keyname.'">'.$keyname.'</option>'; 
}
 ?>
</select>

Upvotes: 2

Related Questions