Educación Informática

SPL ArrayObject

Utilizando SPL para manipular un array.


/* 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() . "";
	}
}
catch (Exception $e)
{
	echo $e->getMessage();
}

No Trackback/Pingback

Pinging is currently not allowed.

Comments are closed.