Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

309
Vistas
Which is a better way to loop through an array in PHP?
foreach ( $arr as $a ){
   //some code
}

or

while ( $a = array_shift($arr) ){
   //some code
}

Which one should I use?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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().

over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda