Created
August 7, 2019 18:44
-
-
Save parzibyte/2bc30defbfebf1bf9d1c5e4cb7ef3261 to your computer and use it in GitHub Desktop.
Buscar objeto en arreglo de PHP - 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 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