Getting the key of the only element in a PHP array

前端 未结 6 825
暖寄归人
暖寄归人 2021-01-17 12:17

The key of the associative array is dynamically generated. How do I get the \"Key\" of such an array?

$arr = array (\'dynamic_key\' => \'Value\');
         


        
6条回答
  •  离开以前
    2021-01-17 13:10

    edit: http://php.net/each says:

    each

    Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.


    Using key() is fine.
    If you're going to fetch the value anyway you can also use each() and list().

    $arr = array ('dynamic_key' => 'Value');
    list($key, $value) = each($arr);
    echo $key, ' -> ', $value, "\n";
    

    prints dynamic_key -> Value

提交回复
热议问题