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.

6
  • what have you try so far? Commented Oct 23, 2018 at 12:42
  • 1
    Hint: have a look at array_keys function. Commented Oct 23, 2018 at 12:43
  • I would like to create a html select and for every "option" to have that values from the array structure you see above. Commented Oct 23, 2018 at 12:43
  • There is no need to repeat more or less exactly what you already said in your question in a comment. You were asked what you have tried so far, so answer that please. Commented Oct 23, 2018 at 12:46
  • You are not making a lot of sense here, even on your second atempt at this question. Can you at least show us an example of the output you would like to generate please Commented Oct 23, 2018 at 12:46

3 Answers 3

2

you can just use Foearch :

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

echo '<option value="'.$keyname.'">'.$keyname.'</option>'; 
}
 ?>
</select>
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

I recieve in my error_log: Invalid argument supplied for foreach()
When you copy past my code ? before your loop please var_dump your variable and confirm is really array.
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);

2 Comments

Thank you for the answer! Do you know why your sample works but my real array not? vilavaleaprahovei.ro/kimea/allMarks.php Error: "array_keys() expects parameter 1 to be array" and "Invalid argument supplied for foreach()"
@Gafwebmaster it works for me. Actually, I have tried with your actual array 3v4l.org/eO3Kh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.