我的问题完全如上所述.
我想知道是否足够"信任"数组的内部指针总是指向它的第一个元素,无论如何,只需使用它:
$bar = current($foo);
Run Code Online (Sandbox Code Playgroud)
或者,如果我没有机会,首先将数组的内部指针重置为第一个元素,然后再使用它:
reset($foo);
$bar = current($foo);
Run Code Online (Sandbox Code Playgroud)
我问的原因是因为如果current()函数本身不可靠,它可能会给最终用户带来误导性信息,我宁愿避免使用以下主题的任何电子邮件:
"What is this? I don't even..."
Run Code Online (Sandbox Code Playgroud)
我相信你明白了.:)
编辑:
我知道current()函数的要点是访问数组当前内部指针的位置.我的问题是,当没有其他函数调用应该移动内部指针时,内部指针是否保证在数组创建后立即指向数组的第一个元素.
关键current()是要访问当前数组内部指针所在的元素.如果你想使用一个总是返回第一个元素的函数,那么reset()该数组的内部指针(它也返回第一个元素中的值,所以你不需要current()在那之后调用),或者使用$foo[0](不移动)指针,仅用于正确排序,数字索引的数组).
随着说,回答你的问题,current() 是保证返回您使用创建后立即数组的第一个元素array(...)符号.从功能的手动示例current():
<?php
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
...
Run Code Online (Sandbox Code Playgroud)
并从reset()函数的手动示例:
<?php
$array = array('step one', 'step two', 'step three', 'step four');
// by default, the pointer is on the first element
echo current($array) . "<br />\n"; // "step one"
...
Run Code Online (Sandbox Code Playgroud)