获取每个数组元素的键值

时间:2018-09-28 06:42:31

标签: php arrays

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

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

结果应为:

晚上
太阳

谢谢

3 个答案:

答案 0 :(得分:2)

您应该使用:

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

答案 1 :(得分:1)

您可以这样打印密钥名称:

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

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

输出:

night 
sun 
she 

答案 2 :(得分:0)

尝试

$key包含您所有的数组键,$value包含您所有的值

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

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