Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/2bc30defbfebf1bf9d1c5e4cb7ef3261 to your computer and use it in GitHub Desktop.
Save parzibyte/2bc30defbfebf1bf9d1c5e4cb7ef3261 to your computer and use it in GitHub Desktop.
<?php
function busquedaSecuencialEnArregloDeObjetos(&$arreglo, $codigoDeBarrasBuscado){
foreach($arreglo as $indice => $producto){
/*
Recorrer todo el arreglo e ir comparando elemento por elemento.
Si lo encontramos, el foreach se detiene y se regresa el índice
*/
if($producto->getCodigo() === $codigoDeBarrasBuscado){
return $indice;
}
}
/*
Si después de recorrer todo el arreglo no lo encontramos,
entonces devolvemos -1
*/
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment