user1551496
user1551496

Reputation: 169

Getting no array key

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>

Upvotes: 0

Views: 56

Answers (3)

Awlad Liton
Awlad Liton

Reputation: 9351

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>

Upvotes: 1

Anand Solanki
Anand Solanki

Reputation: 3425

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>

Upvotes: 1

Alma Do
Alma Do

Reputation: 37365

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)

Upvotes: 4

Related Questions