Esta entrada también está disponible en: Inglés
Utilizando SPL para manipular un array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* Arreglo */ $personas = array("Carlos", "Carolina", "José", "Manuel", "Sandra"); try { /* Creamos el objeto SPL */ $arrayObjSPL = new ArrayObject($personas); /* Modificamos el elemento 4 */ $arrayObjSPL->offsetSet(4, "Claudia"); /* Eliminamos el elemento 2 */ $arrayObjSPL->offsetUnset(2); /* Iteramos sobre el arreglo con el objeto SPL */ for($iterator = $arrayObjSPL->getIterator(); $iterator->valid(); $iterator->next()) { /* Mostramos el elemento */ echo $iterator->key() . " -> " . $iterator->current() . "<br />"; } } catch (Exception $e) { echo $e->getMessage(); } |