0
$arr = ['night' => 'black', 'sun' => 'light', 'she' => 'gold'];

foreach ($arr as $el) {
    echo ... . '<br>';
}

result should be:

night
sun
she

Thanks

2
  • 2
    foreach ($arr as $key => $el).. Then echo the $key variable Commented Sep 28, 2018 at 6:43
  • foreach ($arr as $key => $val) probably is what you are looking for. Or array_keys() php.net/manual/en/function.array-keys.php Commented Sep 28, 2018 at 6:44

3 Answers 3

2

You can print key name like this:

$arr = ['night' => 'black', 'sun' => 'light', 'she' => 'gold'];

foreach ($arr  as $key => $el) {
    echo  $key. ' <br>';
}

Output:

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

Comments

2

You should use:

<?php $array_keys = array_keys($your_array);

1 Comment

Implode the keys with <br> as the glue
0

try this

$key contain all your array key and $value contain all your value

$arr = ['night' => 'black', 'sun' => 'light', 'she' => 'gold'];

foreach ($arr as $key=>$value) {
    echo $key."=".$value."<br>";
}

2 Comments

output is not as desired
i just display both key and value. thanks for review.

Your Answer

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