我试图在如下所示的关联数组中获取数组的第一个键。我知道我可以使用key,但我读过(在这个网站上),它的效率较低。
所以我正在使用current(array_keys($data)).
还有另一种方法吗?当我使用时,我总是得到第一把钥匙current(array_keys($data))吗?这就是我害怕的。
我正在使用 php 5.3.18。这是脚本开始的方式。
<?php
$json = '{"user":"norman","city":"san jose","type":"editor"}';
$data = json_decode($json, true);
echo current(array_keys($data));
//Output I need is "user"
?>
Run Code Online (Sandbox Code Playgroud)
echo current(array_keys($data)); 只是使用是一个漫长的过程 key
echo key($data);
Run Code Online (Sandbox Code Playgroud)
笔记
$data = json_decode($json, true);会重置数组...所以不需要reset再次调用