您好我正在编写一个系统,我需要一个函数来获取和删除数组的第一个元素。这个数组有数字,即
0,1,2,3,4,5
如何循环遍历此数组并且每次传递都获取值,然后从数组中删除它,这样在5轮结束时数组将为空。
提前致谢
答案 0 :(得分:18)
您可以使用array_shift
:
while (($num = array_shift($arr)) !== NULL) {
// use $num
}
答案 1 :(得分:6)
您可以尝试使用foreach / unset,而不是array_shift。
$array = array(0, 1, 2, 3, 4, 5);
foreach($array as $value)
{
// with each pass get the value
// use method to doSomethingWithValue($value);
echo $value;
// and then remove that from the array
unset($array[$value]);
}
//so at the end of 6 rounds the array will be empty
assert('empty($array) /* Array must be empty. */');
?>