Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 18:42
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/067de2cf3d0e3863363e831bdc7ea820 to your computer and use it in GitHub Desktop.
Save parzibyte/067de2cf3d0e3863363e831bdc7ea820 to your computer and use it in GitHub Desktop.
Búsqueda secuencial en arreglos con PHP, método manual - https://parzibyte.me/blog/2018/10/12/busqueda-secuencial-arreglo-php/
<?php
function busquedaSecuencialEnArregloDeEnteros(&$arreglo, $numeroBuscado){
foreach($arreglo as $indice => $numero){
/*
Recorrer todo el arreglo e ir comparando elemento por elemento.
Si lo encontramos, el foreach se detiene y se regresa el índice
*/
if($numero === $numeroBuscado){
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