dpbyr64224 2013-04-29 12:31
浏览 608
已采纳

获取关联数组中的第一个键

I'm trying to get the first key of an array in an associative array like below. I know I can use key, but I read (on this site), that's it's less efficient.

So I'm using current(array_keys($data)).

Is there another way of doing this? Will I always get the first key when I use current(array_keys($data))? That's what I scared off.

I'm using php 5.3.18. This is the way the script starts off.

<?php
$json = '{"user":"norman","city":"san jose","type":"editor"}';

$data = json_decode($json, true);

echo current(array_keys($data));
//Output I need is "user"
?>
  • 写回答

3条回答 默认 最新

  • doubi1818 2013-04-29 12:39
    关注

    echo current(array_keys($data)); is a long process just use key

     echo key($data);
    

    See Live Demo

    Note

    $data = json_decode($json, true); would reset the array ... so no need to call reset again

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧