【发布时间】:2011-12-29 21:48:55
【问题描述】:
我创建了一个简单的测试用例来复制我遇到的问题。
我正在使用next() 和current() 函数遍历一个二维数组,并希望将数组指针设置为特定位置。因此,给定一个变量名为$food 的二维数组,数组结构如下:
array
0 => <-- POINTER LOCATION
array
0 => string 'apple' <-- POINTER LOCATION
1 => string 'orange'
1 =>
array
0 => string 'onion'
1 => string 'carrot'
...以及下面的代码sn -p:
// move the inner array's pointer once
$should_be_orange = next(current($food));
// now check that inner array's value
$should_still_be_orange = current(current($food));
...为什么$should_be_orange"orange"的值是$should_still_be_orange"apple"的值?这是因为current() 函数返回内部数组的副本,谁的指针被迭代,然后被销毁(保持原始 数组不变)?还是我只是做错了我没有注意到的事情?
在问题的根源上,如果您不知道外部数组的键(并且必须使用current() 函数来获取外部数组的指针位置),您如何移动内部数组的指针?
【问题讨论】:
标签: php arrays pointers multidimensional-array