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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题
  • ¥20 请问有人知道怎么用工艺库里面的sdb文件通过virtuoso导出来library里面每个cell的symbol吗?