dsxrq28228 2019-01-31 19:14
浏览 66
已采纳

如何在foreach中复制当前数组键[复制]

This question already has an answer here:

In my PHP code I want to check inside foreach iteration what is the current array key (In code below: key1/key2/key3/key4/key5).

I have assoc array which looks like this:

var_dump($myArray);

array(5) {

    ["key1"]=> array(2) {....}

    ["key2"]=> array(2) {....}

    ["key3"]=> array(2) {....}

    ["key4"]=> array(2) {....}

    ["key5"]=> array(2) {....}

}

foreach($myArray as $key) {
    echo key($myArray);
}

For example this only returns: key1key1key1key1key1

My desired output should look like: key1key2key3key4key5

I've been searching for a while but I can't find any smart solution.

Thank you :)

</div>
  • 写回答

1条回答 默认 最新

  • dsb238100 2019-01-31 19:18
    关注

    PHP.net foreach documentation

    foreach ($variable as $key => $value) {
      echo $key .':'. $value;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?