foreach ( $arr as $a ){
//some code
}
or
while ( $a = array_shift($arr) ){
//some code
}
Which one should I use?
foreach ( $arr as $a ){
//some code
}
or
while ( $a = array_shift($arr) ){
//some code
}
Which one should I use?
According to php official website comments section,
Using array_shift over larger array was fairly slow. It sped up as the array shrank, most likely as it has to reindex a smaller data set.
array_shift() requires a re-index process on the array, so it has to run over all the elements and index them.
If the array is too large, avoid using array_shift().
Using foreach is the preferred option. With it, you can bypass any structure that implements Traversable. If you have an indexed array, it is preferable to use for. The difference between ville and foreach shouldn't be too different. Preferred foreach. I also advise you to navigate your code, depending on the case.