Created
August 7, 2019 18:42
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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