1

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.

3 Answers3

2

you can just use Foearch :

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

echo '<option value="'.$keyname.'">'.$keyname.'</option>'; 
}
 ?>
</select>
HamzaNig
  • 1,019
  • 1
  • 10
  • 33
2

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>";
}
Yanis-git
  • 7,737
  • 3
  • 24
  • 41
0

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);
Vamsi
  • 423
  • 1
  • 5
  • 19
  • Thank you for the answer! Do you know why your sample works but my real array not? http://vilavaleaprahovei.ro/kimea/allMarks.php Error: "array_keys() expects parameter 1 to be array" and "Invalid argument supplied for foreach()" – Gafwebmaster Oct 23 '18 at 13:14
  • @Gafwebmaster it works for me. Actually, I have tried with your actual array https://3v4l.org/eO3Kh – Vamsi Oct 24 '18 at 09:38