offsetSet(4, "Claudia"); /* Eliminamos el elemento 2 */ $arrayObjSPL->offsetUnset(2); /* Iteramos sobre el arreglo con […]" /> SPL Array – Juan Garcés

Juan Garcés

Personal Blog

SPL Array

febrero 7th, 2013

Esta entrada también está disponible en: Inglés

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(); }

Juan Garcés

Personal Blog