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>

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

Comments

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>

1 Comment

Pl accept answer, So others will get help. Thanks :)
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>

Comments

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.