0

Hey I'm trying to get the key of my array in a foreach. But got this error Warning: array_keys() expects parameter 1 to be array, string given on line 10

Here is my array:

$status_de = array
(
    '1' => 'Anfrage',
    '2' => 'Angebot',
    '3' => 'Abgeschlossen'  
);

Here is my code:

<select name="land">
    <?php foreach ($status_de as $status) {
      echo "<option value='" . array_keys($status) . "'>" . $status . "</option>";
    }
    ?>
</select>
user1551496
  • 169
  • 2
  • 12

3 Answers3

4

You should use:

<?php foreach ($status_de as $key=>$status) {
  echo "<option value='" . $key . "'>" . $status . "</option>";
}
?>

as array_keys() will return array containing all keys (so not applicable to use with strings operators)

Alma Do
  • 37,009
  • 9
  • 76
  • 105
1

Try this:

foreach loop will get key and value pair, so you can directly use it. No need any function to get those.

<select name="land">
    <?php foreach ($status_de as $key => $value) {
      echo "<option value='" . $key . "'>" . $value . "</option>";
    }
    ?>
</select>
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
1

You can't do like this because array_keys expected an array. In your scenario you give a string.

try like this:

<select name="land">
    <?php foreach ($status_de as $k =>$v) {
      echo "<option value='" . $k . "'>" . $v . "</option>";
    }
    ?>
</select>
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53