嗨,我正在编写一个系统,我需要一个函数来获取和删除数组的第一个元素.这个数组有数字即
0,1,2,3,4,5
我如何循环遍历此数组并且每次传递获取值,然后从数组中删除它,以便在5轮结束时数组将为空.
提前致谢
Tim*_*per 18
你可以使用array_shift这个:
while (($num = array_shift($arr)) !== NULL) {
// use $num
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用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. */');
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13704 次 |
| 最近记录: |